【问题标题】:Using R to create a data table使用 R 创建数据表
【发布时间】:2019-02-04 23:15:02
【问题描述】:

我想在 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】:

    有趣的问题。这是我根据 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()
    

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 2020-03-21
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2022-01-12
      相关资源
      最近更新 更多