gpt4 book ai didi

r - 如何将列表输出到R中的文件

转载 作者:行者123 更新时间:2023-12-04 01:42:50 24 4
gpt4 key购买 nike

假设有一个带有作者的书籍列表,在将数据读入列表“LS”后,我尝试将其输入到文件中,输出为

> write.table(LS, "output.txt")
Error in data.frame(..., title = NULL, :
arguments imply differing number of rows: 1, 0

> write(LS, "output.txt")
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'

我能够使用 dput 但我希望数据格式正确(整个文件中没有重复关键字的冗余)。有什么建议?
谢谢

更新
dput(头(LS,2))
list(structure(list( title = "Book 1", 
authors = list(structure(c("Pooja", "Garg"),
.Names = c("forename","surname")),
structure(c("Renu", "Rastogi"),
.Names = c("forename","surname")))),
.Names = c("title", "authors")),

structure(list( title = "Book 2",
authors = list(structure(c("Barry", "Smit"), .Names = c("forename",
"surname")), structure(c("Tom", "Johnston"), .Names = c("forename",
"surname")))), .Names = c("title", "authors")))

最佳答案

您可以先将列表转换为数据框:

LS.df = as.data.frame(do.call(rbind, LS))

或者
LS.df = as.data.frame(do.call(cbind, LS))

然后你可以简单地用 write.csv 或 write.table 保存 LS.df

关于r - 如何将列表输出到R中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12718472/

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