生成范围内随机年份的列

时间:2019-11-16 10:48:07

标签: r data.table

我有一个非常简单的问题,对此我找不到任何答案。例如,我要创建一个示例,我想给下面的data.table列,其中列的随机年份在一定范围内,例如2004-2010。

library(data.table)
set.seed(1)
DT <- data.table(panelID = sample(50,50),                                                    # Creates a panel ID
                      Country = c(rep("Albania",30),rep("Belarus",50), rep("Chilipepper",20)),       
                      some_NA = sample(0:5, 6),                                             
                      some_NA_factor = sample(0:5, 6),         
                      Group = c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)),
                      norm = round(runif(100)/10,2),
                      Income = round(rnorm(10,-5,5),2),
                      Happiness = sample(10,10),
                      Sex = round(rnorm(10,0.75,0.3),2),
                      Age = sample(100,100),
                      Educ = round(rnorm(10,0.75,0.3),2))           
DT [, uniqueID := .I]                                                                        # Creates a unique ID     
DT[DT == 0] <- NA                                                                            # https://stackoverflow.com/questions/11036989/replace-all-0-values-to-na
DT$some_NA_factor <- factor(DT$some_NA_factor)

1 个答案:

答案 0 :(得分:2)

我们可以使用sample2004:2010replace = TRUE之间选择随机年份。

library(data.table)
DT[, random_year := sample(2004:2010, .N, replace = TRUE)]