Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 生成虚构的面板数据集_R_Data.table - Fatal编程技术网

R 生成虚构的面板数据集

R 生成虚构的面板数据集,r,data.table,R,Data.table,我想创建一个虚构的面板数据集,包含100个面板观察(50对),变量为:panelID、country和year。这意味着每个panelID应该有相同的国家,但有不同的年份(哪一年不重要,但假设相隔4年) 我试着做了以下几件事,但没有成功 panelID = c(1:50) year= c(2001:2010) country = c("NLD", "GRC", "GBR") DT <- data.table(expand.grid(panelID, year, country))

我想创建一个虚构的面板数据集,包含100个面板观察(50对),变量为:panelID、country和year。这意味着每个panelID应该有相同的国家,但有不同的年份(哪一年不重要,但假设相隔4年)

我试着做了以下几件事,但没有成功

panelID = c(1:50)   
year= c(2001:2010)
country = c("NLD", "GRC", "GBR")
DT <- data.table(expand.grid(panelID, year, country))
panelID=c(1:50)
年份=c(2001:2010)
国家=c(“全国民主联盟”、“GRC”、“GBR”)

DT我认为这是预期的结果:

panelID = c(1:50)   
year= c(2001:2010)
country = c("NLD", "GRC", "GBR")

n <- 2

library(data.table)
set.seed(123)
data.table(panelID = rep(sample(panelID), each = n),
           country = rep(sample(country, length(panelID), replace = T), each = n),
           year = c(replicate(length(panelID), sample(year, n))))

     panelID country year
  1:      31     GBR 2010
  2:      31     GBR 2005
  3:      15     NLD 2005
  4:      15     NLD 2008
  5:      14     GRC 2003
  6:      14     GRC 2002
  7:       3     NLD 2010
  8:       3     NLD 2002
  9:      42     NLD 2010
 10:      42     NLD 2006
... 100 rows total
panelID=c(1:50)
年份=c(2001:2010)
国家=c(“全国民主联盟”、“GRC”、“GBR”)
N
panelID = c(1:50)   
year= c(2001:2010)
country = c("NLD", "GRC", "GBR")

n <- 2

library(data.table)
set.seed(123)
data.table(panelID = rep(sample(panelID), each = n),
           country = rep(sample(country, length(panelID), replace = T), each = n),
           year = c(replicate(length(panelID), sample(year, n))))

     panelID country year
  1:      31     GBR 2010
  2:      31     GBR 2005
  3:      15     NLD 2005
  4:      15     NLD 2008
  5:      14     GRC 2003
  6:      14     GRC 2002
  7:       3     NLD 2010
  8:       3     NLD 2002
  9:      42     NLD 2010
 10:      42     NLD 2006
... 100 rows total
#for 4 years apart
set.seed(123)
years <- sample(2001:2006, length(panelID), replace = T)

set.seed(123)
data.table(panelID = rep(sample(panelID), each = n),
           country = rep(sample(country, length(panelID), replace = T), each = n),
           year = c(t(matrix(c(years, years+4), ncol = n))))