从R中的列表导出单独的文本文件

Joe*_*oeF 1 loops r export-to-text

我有4951个命名元素的大量列表.这些元素中的每一个基本上都是字母(即它们是字符串字符).我想要做的是将列表中的每个元素导出为单独的文本文件,并在列表中使用与其名称对应的文件名.

我所拥有的简单版本是:

 letter1 <- c("here is some text")
 letter2 <- c("and here is some more text")
 letter3 <- c("and this is the final one")

 list <- list(letter1 = letter1, letter2 = letter2, letter3 = letter3)
Run Code Online (Sandbox Code Playgroud)

我希望得到以下内容:

letter1.txt,其内容为"here is some text"
letter2.txt,其内容为"这里是更多文本"
letter3.txt,其内容为"这是最后一个"

我想我应该使用一个循环.但是,我不知道如何超越这个:

for (i in 1:length(list)){

}
Run Code Online (Sandbox Code Playgroud)

Pie*_*une 5

for (i in 1:length(list)) { write.csv(list[i], file=paste0(names(list)[i], ".txt")) }

编辑

如果您需要函数中的输出目录:

 write.csv(list[i], file=paste0("output/", names(list)[i], ".txt"))
Run Code Online (Sandbox Code Playgroud)