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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12718472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:54:25  来源:igfitidea点击:

How to output a list to file in R

rlist

提问by Thomas Lee

Assume having a list of books with authors , after reading the data into a list "LS" I tried to enter it into a file and the output was

假设有一个带有作者的书籍列表,在将数据读入列表“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'

I was able to use dput but I would like the data to be formatted well (no redundancy of repeated keywords all over the file). Any suggestions? Thanks

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

UPDATEdput( head (LS, 2))

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

采纳答案by Ali

You may first convert your list to a data frame:

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

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

Or

或者

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

Then you can simply save LS.df with write.csv or write.table

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

回答by mnel

Using the data you provided and rjson

使用您提供的数据和 rjson

library(rjson)

# write them to a file
cat(toJSON(LS), file = 'LS.json')


LS2 <- fromJSON('LS.json')


# some rearranging to get authors back to being a data.frame

LS3 <- lapply(LS2, function(x) { x[['authors']] <-  lapply(x[['authors']], unlist); x})

identical(LS, LS3)

## TRUE

The file looks like

该文件看起来像

[{"title":"Book 1","authors":[{"forename":"Pooja","surname":"Garg"},{"forename":"Renu","surname":"Rastogi"}]},{"title":"Book 2","authors":[{"forename":"Barry","surname":"Smit"},{"forename":"Tom","surname":"Johnston"}]}]

if you want each book on a separate line then you can use

如果您希望每本书都在单独的一行上,那么您可以使用

.json <-  lapply(LS, toJSON)
# add new lines and braces

.json2 <- paste0('[\n', paste0(.json, collapse = ', \n'), '\n]')
 cat(.json)
[
{"title":"Book 1","authors":[{"forename":"Pooja","surname":"Garg"},{"forename":"Renu","surname":"Rastogi"}]}, 
{"title":"Book 2","authors":[{"forename":"Barry","surname":"Smit"},{"forename":"Tom","surname":"Johnston"}]}
]

回答by Raimi bin Karim

I use the RJSONIO package.

我使用 RJSONIO 包。

library(RJSONIO)

exportJSON <- toJSON(LS)
write(exportJSON,"LS.json")

回答by Shahryar

It's better to use format()

最好使用 format()

LS.str <- format(LS)

LS.str <- format(LS)