R 重复行数据表

R 重复行数据表,r,data.table,R,Data.table,我有数据“dataHAVE”,并试图生成“dataWANT”,基本上复制每个“学生”计数的次数,如“dataWANT”所示。我尝试按照上面data.table中所示的方法进行操作,因为这是我寻求的解决方案,但得到的结果是错误的 错误:“setDT(dat)dat”中出现意外符号 我无法解决,非常感谢。试试: library(data.table) dataHAVE=data.frame("student"=c(1,2,3), "score" = c(10,

我有数据“dataHAVE”,并试图生成“dataWANT”,基本上复制每个“学生”计数的次数,如“dataWANT”所示。我尝试按照上面data.table中所示的方法进行操作,因为这是我寻求的解决方案,但得到的结果是错误的

错误:“setDT(dat)dat”中出现意外符号

我无法解决,非常感谢。

试试:

library(data.table)
dataHAVE=data.frame("student"=c(1,2,3),
                    "score" = c(10,11,12),
                "count"=c(4,1,2))


dataWANT=data.frame("student"=c(1,1,1,1,2,3,3),
                    "score"=c(10,10,10,10,11,12,12),
                    "count"=c(4,4,4,4,1,2,2))

setDT(dataHAVE)dataHAVE[rep(1:.N,count)][,Indx:=1:.N,by=student]
输出:

setDT(dataHAVE)[rep(1:.N,count)]
如前所述,您还可以替换
1:.N
并执行
setDT(dataHAVE)[dataHAVE[,rep(.I,count)]

仅供参考,
tidyr
中还有一个很好的函数,可以做类似的事情:

   student score count
1:       1    10     4
2:       1    10     4
3:       1    10     4
4:       1    10     4
5:       2    11     1
6:       3    12     2
7:       3    12     2

这是一个基本的R解决方案

tidyr::uncount(dataHAVE, count, .remove = FALSE)
dataWANT<-do.call(rbind,
                  c(with(dataHAVE,rep(split(dataHAVE,student),count)),
                    make.row.names = FALSE))
> dataWANT
  student score count
1       1    10     4
2       1    10     4
3       1    10     4
4       1    10     4
5       2    11     1
6       3    12     2
7       3    12     2