我有一個包含100個列名的大型csv文件new.dat
。我想分割new.dat
每列名稱保留所有新子集中的第一列寫爲.csv
。如何按列拆分大數據框並將其寫入單個CSV
new.dat
new.dat <- structure(list(Sequence = c("AAAAAACCTGTTCTGATA", "AAAAAAGGCTGTTACTGAGC",
"AAAAACATTCGAGCGAGATCTCT", "AAAAACCTCGACTTCGGAAG", "AAAAAGCTCGTAGTTGAA",
"AAAAAGCTCGTAGTTGAAC"), WT1 = c("84", "104", "80", "35", "112",
"350"), WT2 = c("149", "478", "502", "186", "577", "911"), AGO1 = c("32",
"147", "433", "51", "258", "353"), AGO2 = c("37", "222", "355",
"85", "408", "420"), DCL1 = c("56", "185", "291", "48", "167",
"273"), DCL2 = c("59", "176", "294", "31", "185", "245"), NAs = c(0L,
0L, 0L, 0L, 0L, 0L)), .Names = c("Sequence", "WT1", "WT2", "AGO1",
"AGO2", "DCL1", "DCL2", "NAs"), row.names = c(NA, 6L), class = "data.frame")
所以從new.dat
數據結果應該有七個的CSV文件。第一個CSV WT1.csv
與Sequence
和WT1
列,第二csv文件WT2.csv
與Sequence
和WT2
列等等..
這是我試過的代碼。請建議我在這裏失蹤。 謝謝
for (name in colnames(new.dat[-1])){
tmp=subset(new.dat$Sequence, colnames==name)
fn= name
#Save the CSV file
write.csv(tmp,fn,row.names=FALSE)
}
難道你的意思是說你有多個'WT1'列?如果是這樣,你有任何模式。這些是前綴嗎? – akrun
@akrun不,我想在所有新的csv文件中保留「序列」列和一個額外的列。列名是唯一的且不重複的。 – MAPK
如果是這樣的話,循環遍歷列和cbind與第一列,即'lst < - Map(函數(x,y,z)setNames(cbind(x,y),c(「Sequence」,z)) ,列表(new.dat [1]),new.dat [-1],名稱(new.dat)[ - 1])'並將其寫入csv – akrun