Elegant way to do the inverse of aggregation

rogojel

TS Contributor
#1
hi,
I have a data frame with several factor variables and one numeric variable that gives the number of times a row with the given combination of factor values was obseved. I would like to disaggregate (if there is a such a word) the data frame to get one where a factor combination is repeteated as many times as it was observed.

Being an ex-C programer the obvouus and easy way for me would be to use two nested loops. I am just wondering if there would be a more R-ish way to do this?

thanks a lot
 

JesperHP

TS Contributor
#2
Perhaps this is it ....
Code:
a1=c("a","b","c")
a2=c("d","a","b")
mydata=data.frame(a1,a2,count=c(3,4,2))


N=nrow(mydata)
M=ncol(mydata)
K=M-1


index=rep(1:N,mydata$count)
index
newdata=mydata[index,1:K]
newdata