簡體   English   中英

自動化write.csv

[英]Automate write.csv

我有一個大的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列之外的所有列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM