在R中的面板数据中添加额外的观察

时间:2011-12-02 23:05:32

标签: r indexing panel

我正在尝试为我的面板数据添加额外的年份。只是想知道你们是否有任何想法快速做到这一点。请记住,我的真实数据是T = 6,i = 4000。

# Here is my input
data = data.frame(time=c(30,40,50,30,40,50,30,40,50), 
                  id=c(1,1,1,2,2,2,3,3,3), 
                  d=c(1,4,7,8,14,2,41,11,61))
# declare panel data individ and time
pd = pdata.frame(data, c("id","time"), drop.index=FALSE)

#this is what I want out... 
data.out = data.frame(time=c(30,40,50,60,30,40,50,60,30,40,50,60), 
                      id=c(1,1,1,1,2,2,2,2,3,3,3,3), 
                      d=c(1,4,7,8,9,14,2,41,50,11,61,70))
# declare panel data individ and time
pd.data.out = pdata.frame(data.out, c("id","time"), drop.index=FALSE)    

2 个答案:

答案 0 :(得分:2)

我不太确定你在做什么,但这可能会有所帮助:

data = data.frame(time=c(30,40,50,30,40,50,30,40,50), 
                  id=c(1,1,1,2,2,2,3,3,3), 
                  d=c(1,4,7,8,14,2,41,11,61))
newdata = data.frame(time=c(60,60,60), 
                     id=c(1,2,3), 
                     d=c(9,50,70))
combodata = rbind(data,newdata)
data.out = combodata[order(combodata$id,combodata$time), ]
rownames(data.out) = NULL

生产

> data.out
   time id  d
1    30  1  1
2    40  1  4
3    50  1  7
4    60  1  9
5    30  2  8
6    40  2 14
7    50  2  2
8    60  2 50
9    30  3 41
10   40  3 11
11   50  3 61
12   60  3 70

我认为这是timeid所需要的,但d略有不同。如果不需要对行进行排序,则可以将代码的最后三行压缩为

data.out = rbind(data,newdata)

答案 1 :(得分:0)

知道了......只需创建新的时间和id data.frame并合并到其中。

time = rep(c(unique(as.numeric(as.character(pd$time))),max(as.numeric(as.character(pd$time)))+10), length(unique(pd$id)))
id   = rep( unique(pd$id), each=max(as.numeric(as.character(pd$id)))+1)
data2 = data.frame(time, id)

data.out = merge(data2, pd, all.x=T)
data.out = data.out[with(data.out, order(id,time) ), ]