I have an object of type data.frame like this, but much more:
> head(mydf) id1 id2 n 1 0 1032142 3 2 0 1072163 1 3 0 119323 2
I need to print in the columns of files a1 and a1 , each of which is n times. So that I can get a file like this:
0 1032142 0 1032142 0 1032142 0 1072163 0 119323 0 119323
I tried the following solutions, but they use explicit for loops and are incredibly slow (it takes several days to complete my data ...):
for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) write.table( mydf[j,c("id1","id2")], file="trials", append=T, row.names= F, col.names=F )
Another is trying to create a new data.frame with multiplied rows, but it runs even slower.
towrite=data.frame(); for (j in 1:(nrow(mydf))) for (i in 1:(mydf[j,"n"])) towrite=rbind(towrite,mydf[j,c("id1","id2")])
What is the easiest and fastest way to resolve this with R?
source share