Export a list into a CSV or TXT file in R

后端 未结 8 1970
遥遥无期
遥遥无期 2020-11-27 16:02

I\'m sorry to ask this question. I\'m very noob with R. I know there is a lot of threads which are related to the very same problem. I understood that we cannot export a tab

相关标签:
8条回答
  • 2020-11-27 16:08

    I think the most straightforward way to do this is using capture.output, thus;

    capture.output(summary(mylist), file = "My New File.txt")
    

    Easy!

    0 讨论(0)
  • 2020-11-27 16:14

    You can simply wrap your list as a data.frame (data.frame is in fact a special kind of list). Here is an example:

    mylist = list() 
    mylist[["a"]] = 1:10 
    mylist[["b"]] = letters[1:10]
    write.table(as.data.frame(mylist),file="mylist.csv", quote=F,sep=",",row.names=F)
    

    or alternatively you can use write.csv (a wrapper around write.table). For the conversion of the list , you can use both as.data.frame(mylist) and data.frame(mylist).

    To help in making a reproducible example, you can use functions like dput on your data.

    0 讨论(0)
  • 2020-11-27 16:14

    I export lists into YAML format with CPAN YAML package.

    l <- list(a="1", b=1, c=list(a="1", b=1))
    yaml::write_yaml(l, "list.yaml")
    

    Bonus of YAML that it's a human readable text format so it's easy to read/share/import/etc

    $ cat list.yaml
    a: '1'
    b: 1.0
    c:
      a: '1'
      b: 1.0
    
    0 讨论(0)
  • 2020-11-27 16:25

    Check out in here, worked well for me, with no limits in the output size, no omitted elements, even beyond 1000

    Exporting large lists in R as .txt or .csv

    0 讨论(0)
  • 2020-11-27 16:30

    using sink function :

    sink("output.txt")
    print(mylist)
    sink()
    
    0 讨论(0)
  • 2020-11-27 16:32

    cat(capture.output(print(my.list), file="test.txt"))

    from R: Export and import a list to .txt file https://stackoverflow.com/users/1855677/42 is the only thing that worked for me. This outputs the list of lists as it is in the text file

    0 讨论(0)
提交回复
热议问题