每隔一列拆分一个数据框以创建两个单独的文件 [英] Splitting a dataframe every other column to create two separate files

查看:84
本文介绍了每隔一列拆分一个数据框以创建两个单独的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想(一如既往)使用性能更好但功能等同于以下代码的代码:

I want to (as ever) use code that performs better but functions equivalently 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是一个看起来像这样的数据框:

results.df is a dataframe that looks something thus:

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 patterning in the column headers except that each one is repeated twice consecutively.

我想创建(1)一个仅包含results.df奇数列的文件,以及(2)创建另一个仅包含results.df偶数列的文件.我在上面有一个解决方案,但想知道是否有一种性能更好的方法来实现相同的目标.

I want to create (1) one file with only odd-numbered columns of results.df and (2) another file with only even-numbered columns of results.df. I have one solution above, but was wondering whether there is a better-performing means of achieving the same thing.

想法更新:我当时在想可能有一些切除方法-从内存中删除-每个处理的列,而不只是复制它.这样,数据帧的大小逐渐减小,并可能导致性能提高?

IDEA UPDATE: I was thinking there may be some way of excising - deleting it from memory - each processed column rather than just copying it. This way the size of the dataframe progressively decreases and may result in a performance increase???

推荐答案

代码只是稍短一些,但是...

The code is only slightly 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)]

这篇关于每隔一列拆分一个数据框以创建两个单独的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆