使用R创建数据表

问题描述 投票:-2回答:1

我有一些我想在R做的事,但我甚至不知道如何开始。我想创建一个数据表,比如说8列宽。

我想为每一栏设定条件,即

列值最大值为10,70,100,100,100,100,100,100

列值0,0,0,0,0,0,0,20的最小值

行的总和= 100

步骤说5。

我们的想法是每列向下逐步,直到行= 100,然后它移动到下一行。

预期输出类似于:

10, 70, 20, 0, 0, 0, 0, 0
10, 70, 15, 5, 0, 0, 0, 0
10, 70, 15, 0, 5, 0, 0, 0  
10, 70, 15, 0, 0, 5, 0, 0 
10, 70, 15, 0, 0, 0, 5, 0 
10, 70, 15, 0, 0, 0, 0, 5 
10, 70, 10, 10, 0, 0, 0, 0
 10, 70, 10, 0, 10, 0, 0, 0
......

一旦它遵循这种模式,比如50,000行就会结束计算。

我该怎么做呢?

问候,

他自己

r datatable
1个回答
0
投票

有趣的问题。这是基于chi ^ 2(2 df)分布的随机抽样的我的观点。

这里的一个大问题是每行的总和不是严格的100。

library(dplyr)
library(data.table)

dat <- lapply(1:100, function(x) { # controls number of rows
  dat <- c(runif(1, 0, 0.01), runif(1, 0.3, 2), runif(8, 6, 20)) %>% # tune these, controls where in the distribution the values are coming from
    sort() %>%
    dchisq(df = 3) # samples from sections of a chi square distribution with 2 df
  dat <- plyr::round_any(300*dat, 5, f = round) # rounds to steps of 5 - the '300' scales the responses.
  dat
}) %>%
  as.data.table() %>%
  t()

apply(dat, 1, sum) %>%
  hist()
© www.soinside.com 2019 - 2024. All rights reserved.