out_con <- file("out.txt", "w") write(sprintf("This is line %d.\n",1),out_con,append=T) write("This is line 2.",out_con,append=T) close(out_con)
或者
printer = file("out.txt","w") writeLines("This is line.",con=printer,sep=" ") writeLines("The same line.",con=printer) close(printer)
参考:
http://grokbase.com/t/r/r-help/137hry3mga/r-writing-multiple-lines-to-a-file
也可以用sink
sink("tmp_out.txt") con <- file("raw.txt", "r") line <- readLines(con, n=1) while(length(line) != 0) { c = unlist(strsplit(line, "\t")) if (c[1] == "Contig") { #k_test <- rbind(k_test, c) cat(line) cat("\n") } else if (as.numeric(c[1]) %in% g.out$name) { ##a <- sapply(c, as.numeric) ##k_test <- rbind(k_test, a) cat(line) cat("\n") } line = readLines(con, n=1) } close(con) sink()
时间: 2024-11-02 21:37:51