Split data frame of each other column to create two separate files

I want (more than ever) to use code that works better, but performs functions equivalent to the following:

write.table(results.df[seq(1, ncol(results.df),2)],file="/path/file.txt", row.names=TRUE, sep="\t") write.table(results.df[seq(2, ncol(results.df),2)],file="/path/file2.txt",row.names=TRUE, sep="\t") 

results.df is a data frame that looks like this:

 row.names 171401 171401 111201 111201 1 1 0.8320923 10 0.8320923 2 2 0.8510621 11 0.8510621 3 3 0.1009001 12 0.1009001 4 4 0.9796110 13 0.9796110 5 5 0.4178686 14 0.4178686 6 6 0.6570377 15 0.6570377 7 7 0.3689075 16 0.3689075 

There is no consistent pattern in the column headings, except that each is repeated twice in a row.

I want to create (1) one file with only columns with odd numbers results.df and (2) another file with only even columns with results.df . I have one solution above, but I was wondering if there are more effective means of achieving the same.

IDEA UPDATE: I thought there might be some way to excise - deleting it from memory - each processed column, not just copying it. Thus, the data block size is gradually reduced and may lead to increased performance.

+4
source share
1 answer

The code is a bit shorter, but ...

 # Instead of results.df[seq(1, ncol(results.df), 2] results.df[seq(2, ncol(results.df), 2] #you could use results.df[c(T,F)] results.df[c(F,T)] 
+5
source

Source: https://habr.com/ru/post/1389146/


All Articles