gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:57:42 25 4
gpt4 key购买 nike

我有一个包含 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)

我想要以下内容:

letter1.txt,内容为“here is some text”
letter2.txt,内容为“and here is some more text”
letter3.txt,内容为“这是最后一个”

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

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

}

最佳答案

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"))

关于r - 从 R 中的列表导出单独的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com