R 在磁盘中保存列表
R save list in disk
我有一个列表 var1:
var1 <- list(c("parts of a day", "time in astronomy", "star"), c("tree tall", "pine tree"), c("environmental condition", "climate"))
我想将它保存在磁盘中。例如磁盘中保存的文件,csv文件的每一行必须有如下内容:
"parts of a day"
"time in astronomy"
"star"
"Tree tall"
"pine tree"
"environmental condition"
"climate"
我试过了
capture.output(unlist(var1), file="a1.csv", append=FALSE)
但这是创建以下格式:
[1] "parts of a day" "time in astronomy" "star" "tree tall"
[5] "pine tree" "environmental condition" "climate"
如何解决?代码片段将不胜感激。
谢谢。
你可以试试
cat(paste0("'", unlist(var1), "'"), file='yourfile.txt', sep="\n")
或者不使用 paste
我们可以使用 shQuote
cat(shQuote(unlist(var1)), file='john.txt', sep="\n")
我有一个列表 var1:
var1 <- list(c("parts of a day", "time in astronomy", "star"), c("tree tall", "pine tree"), c("environmental condition", "climate"))
我想将它保存在磁盘中。例如磁盘中保存的文件,csv文件的每一行必须有如下内容:
"parts of a day"
"time in astronomy"
"star"
"Tree tall"
"pine tree"
"environmental condition"
"climate"
我试过了
capture.output(unlist(var1), file="a1.csv", append=FALSE)
但这是创建以下格式:
[1] "parts of a day" "time in astronomy" "star" "tree tall"
[5] "pine tree" "environmental condition" "climate"
如何解决?代码片段将不胜感激。
谢谢。
你可以试试
cat(paste0("'", unlist(var1), "'"), file='yourfile.txt', sep="\n")
或者不使用 paste
我们可以使用 shQuote
cat(shQuote(unlist(var1)), file='john.txt', sep="\n")