R 自动写入.csv

R 自动写入.csv,r,R,我有一个大的data.table df.iso,我想将其拆分并写入单独的文件。是否可以自动执行write.csv功能 > colnames(df.iso) [1] "V1" "Seq" "../trimmed/60G.tally.fasta" [4] "../trimmed/73R.tally.fasta" "../trimmed/246R.tally.fasta" "../trimm

我有一个大的data.table df.iso,我想将其拆分并写入单独的文件。是否可以自动执行write.csv功能

> colnames(df.iso)
 [1] "V1"                          "Seq"                         "../trimmed/60G.tally.fasta" 
 [4] "../trimmed/73R.tally.fasta"  "../trimmed/246R.tally.fasta" "../trimmed/402G.tally.fasta"
 [7] "../trimmed/93G.tally.fasta"  "../trimmed/100R.tally.fasta" "../trimmed/124G.tally.fasta"
[10] "../trimmed/350R.tally.fasta" "../trimmed/126G.tally.fasta" "../trimmed/379R.tally.fasta"
[13] "../trimmed/134R.tally.fasta" "../trimmed/57R.tally.fasta"  "../trimmed/243G.tally.fasta"
[16] "../trimmed/361R.tally.fasta" "../trimmed/388R.tally.fasta" "../trimmed/94R.tally.fasta" 
[19] "../trimmed/270R.tally.fasta" "../trimmed/240G.tally.fasta" "../trimmed/46R.tally.fasta" 
[22] "../trimmed/356R.tally.fasta" "../trimmed/77G.tally.fasta"  "../trimmed/122R.tally.fasta"
[25] "../trimmed/35R.tally.fasta"  "../trimmed/246G.tally.fasta" "../trimmed/367G.tally.fasta"
[28] "../trimmed/93R.tally.fasta"  "../trimmed/305R.tally.fasta" "../trimmed/82R.tally.fasta" 
[31] "../trimmed/48R.tally.fasta"  "../trimmed/377R.tally.fasta" "../trimmed/106R.tally.fasta"
[34] "../trimmed/57G.tally.fasta"  "../trimmed/279G.tally.fasta" "../trimmed/400R.tally.fasta"
[37] "../trimmed/60R.tally.fasta"  "../trimmed/335R.tally.fasta" "../trimmed/192G.tally.fasta"
[40] "../trimmed/239R.tally.fasta" "../trimmed/167G.tally.fasta" "../trimmed/70R.tally.fasta" 
[43] "../trimmed/68R.tally.fasta"  "../trimmed/367R.tally.fasta" "../trimmed/235R.tally.fasta"
[46] "../trimmed/141R.tally.fasta" "../trimmed/342R.tally.fasta" "../trimmed/55R.tally.fasta" 
[49] "../trimmed/356G.tally.fasta" "../trimmed/35G.tally.fasta"  "../trimmed/267G.tally.fasta"
[52] "../trimmed/402R.tally.fasta" "../trimmed/55G.tally.fasta"  "../trimmed/267R.tally.fasta"
[55] "../trimmed/235G.tally.fasta" "../trimmed/301G.tally.fasta" "../trimmed/70G.tally.fasta" 
[58] "../trimmed/106G.tally.fasta" "../trimmed/240R.tally.fasta" "../trimmed/126R.tally.fasta"
[61] "../trimmed/73G.tally.fasta"  "../trimmed/82G.tally.fasta"  "../trimmed/77R.tally.fasta" 
[64] "../trimmed/185R.tally.fasta" "../trimmed/167R.tally.fasta" "../trimmed/124R.tally.fasta"
[67] "../trimmed/270G.tally.fasta" "../trimmed/94G.tally.fasta"  "../trimmed/400G.tally.fasta"
[70] "../trimmed/26R.tally.fasta"  "../trimmed/377G.tally.fasta" "../trimmed/361G.tally.fasta"
[73] "../trimmed/192R.tally.fasta" "../trimmed/141G.tally.fasta" "../trimmed/301R.tally.fasta"
[76] "../trimmed/366R.tally.fasta" "../trimmed/261G.tally.fasta" "../trimmed/46G.tally.fasta" 
[79] "../trimmed/122G.tally.fasta" "../trimmed/243R.tally.fasta" "../trimmed/299R.tally.fasta"
[82] "../trimmed/279R.tally.fasta" "../trimmed/350G.tally.fasta" "../trimmed/388G.tally.fasta"
[85] "../trimmed/335G.tally.fasta" "../trimmed/366G.tally.fasta" "../trimmed/48G.tally.fasta" 
[88] "../trimmed/26G.tally.fasta"  "../trimmed/185G.tally.fasta" "../trimmed/342G.tally.fasta"
[91] "../trimmed/305G.tally.fasta" "../trimmed/261R.tally.fasta" "../trimmed/100G.tally.fasta"
[94] "../trimmed/379G.tally.fasta" "../trimmed/134G.tally.fasta" "../trimmed/68G.tally.fasta" 
[97] "../trimmed/299G.tally.fasta" "../trimmed/239G.tally.fasta"
我使用此函数定义和写入子文件:

 write.table(df.iso[,c("Seq","../trimmed/60G.tally.fasta"), with=FALSE], 
 file="IsomiR_60G.txt",
 sep="\t", quote=FALSE, row.names = FALSE)

我假设您希望根据现有代码编写制表符分隔的文件。为此,您可以:

for (col in colnames(df.iso)[-(1:2)]) {
  filename <- sub("^\\.*/trimmed/([0-9A-Z]+)\\..*$", "IsomiR_\\1.txt", col)
  write.table(df.iso[, c("Seq", col), with = FALSE], 
  file = filename,
  sep="\t", quote=FALSE, row.names = FALSE)
}

这使用for循环遍历除前2列之外的每一列。

它似乎有一个bug,输出的只是列名:x Seq../trimmed/26G.tally。fasta@BioMan你能澄清一下吗?有文件被写入吗?文件被写入了,但其中只有列名。没有其他数据。请记住,我希望写入所有行,而不仅仅是列名。data.frame有300000行。我认为这一定是一个data.table。你能做classdf.iso吗?是的,我用fread阅读classdf.iso[1]data.table data.frame