0
我有我自己的解决方案tp我的问题,但它涉及循环并且相对较慢。任何方式在R中更快地使用它 - 使用一些更高级的软件包? 谢谢!In R:重复数据帧的每个原始数据所需的次数(对于每一行可能不同)
# My data frame:
d <- data.frame(ID=1:10,a = 1:10,b = 5:14)
# Desired number of repeats for each row:
freq = c(2,2,3,3,3,4,5,5,5,6)
# i.e., Raw 1 of d should be repeated 2 times
# Raw 10 of d should be repeated 6 times.
# My current solution - looping through unique values of freq:
d.long<-NULL
myrepeats=unique(freq)
for(i in myrepeats){
onecount<-d[freq==i,]
for(ii in 2:i){
temp<-d[freq==i,]
onecount<-rbind(onecount,temp)
}
d.long<-rbind(d.long,onecount)
}
d.long<-d.long[order(d.long$ID),]
(d.long)
大,非常感谢 - 这是真快,优雅! – user2323534 2013-04-26 12:04:33