在R脚本语言中,如何编写行文本,例如,下面两行

Hello
World

到一个名为output.txt的文件?


当前回答

简单的write.table()怎么样?

text = c("Hello", "World")
write.table(text, file = "output.txt", col.names = F, row.names = F, quote = F)

参数col.names = FALSE和row.names = FALSE确保排除txt中的行名和列名,参数quote = FALSE排除txt中每行开头和结尾的引号。 要将数据读入,可以使用text = readLines("output.txt")。

其他回答

fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)

你可以用一个语句来做

cat("hello","world",file="output.txt",sep="\n",append=TRUE)

实际上你可以用sink()来实现:

sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()

因此做:

file.show("outfile.txt")
# hello
# world

Tidyverse版本与管道和write_lines()从阅读器

library(tidyverse)
c('Hello', 'World') %>% write_lines( "output.txt")

简单的write.table()怎么样?

text = c("Hello", "World")
write.table(text, file = "output.txt", col.names = F, row.names = F, quote = F)

参数col.names = FALSE和row.names = FALSE确保排除txt中的行名和列名,参数quote = FALSE排除txt中每行开头和结尾的引号。 要将数据读入,可以使用text = readLines("output.txt")。