如何用R中的writeLines写入文件

时间:2014-02-05 01:03:41

标签: r

我正在尝试编写文件并收到错误:

这就是我在做的事情:

sites<-c("New York", "Boston", "San Francisco")
p<-1:10
for(i in 1:length(sites)) {
    print(sites[i])
        writeLines(p, paste0(gsub(" ", "_", sites[i]),".txt")
}

我收到此错误:

Error in writeLines(p, gsub(" ", "_", sites[i])) : 
  invalid 'text' argument

1 个答案:

答案 0 :(得分:4)

sites<-c("New York", "Boston", "San Francisco")
p<-1:10
for(i in 1:length(sites)) {
    print(sites[i])
        writeLines(as.character(p), paste0(gsub(" ", "_", sites[i]),".txt"))
}
#----
[1] "New York"
[1] "Boston"
[1] "San Francisco"