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