I have an object of type data.frame like this, but much bigger:
> head(mydf)
id1 id2 n
1 0 1032142 3
2 0 1072163 1
3 0 119323 2
I need to print to a file columns a1 and a1, each of them n times. So that I could get a file like that:
0 1032142
0 1032142
0 1032142
0 1072163
0 119323
0 119323
I tried the following solutions, but they make use of explicit for loops and are incredibly slow (it take few days to finish them with 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 )
The other tries to build a new data.frame with multiplied rows, but it is even slower to run.
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 simplest and fastest way of resolving this under R?
Try subsetting your data and save in one batch:
If your data is numeric, then manipulating the matrix is much faster:
If you managed to manipulate your original data.frame, then you will probably be able to handle the above matrix.