我有一个大文件,在for
循环中一次读取一行。因此,如果我write.table
col.names=TRUE
我将获得x
个标题。所以我想在结尾添加标题(或者在开头,在输出文件的同一行没有两行)。
ifile.valid
:( data.frame)
scaf pos Mother Father SNP3 SNP4 SNP5 SNP6 SNP7
HE639 8 0 0 2 0 0 0 0
到目前为止,我只能在最后添加它:
# #for loop going through all the lines, so this has to come before the loop or the first line of #the output file needs to be ignored
# o.file.c <- file("testdataout.txt",open="at")
# write.table(ifile.valid,file=o.file.c, sep="\t", row.names=FALSE, col.names=FALSE, append=TRUE)
# close(o.file.c)
修改 这是上面为使事情变得简单而生成的文件:
到这个文件的开头我想添加一个标题:
ofile.c <- file("testdataout.txt",open="at")
ifile.c <- file("testdata.csv", open = "r")
header<- read.csv(ifile.c, nrows=1, header=FALSE,sep=",") #scaf pos Mother Father SNP3 SNP4 SNP5 SNP6 SNP7
writeLines(as.character(unlist(header)),ofile.c, sep="\t")
close(ofile.c)
close(ifile.c)