I have a data set which contains counts for every combination of characteristics. A toy example is provided below. So, for example, there are three 18 year old females who make $65,000.
AGE=c(18,19,18,19)
SEX=c("M","F","F","M")
INCOME=c(70000,60000,65000,75000)
COUNT =c(1,2,3,4)
df<-data.frame(AGE,SEX,INCOME,COUNT)
I would like to repeat every observation n times depending on the count. I have accomplished this using a for-loop but I'm finding this very inefficient in R.
df4<-data.frame(AGE=c(),SEX=c(),INCOME=c(),COUNT=c())
for(i in 1:nrow(df)){
n <- df[i,4]
df4<-rbind(df4,df[rep(i, n), ])
}
What is a more efficient way to do this?