53

I have in R a list like this:

> print(head(mylist,2))
[[1]]
[1] 234984  10354  41175 932711 426928

[[2]]
[1] 1693237   13462

Each element of the list has different number of its elements.

I would like to print this list to a text file like this:

mylist.txt
234984  10354  41175 932711 426928
1693237   13462

I know that I can use sink(), but it prints names of elements [[x]], [y] and I want to avoid it. Also because of different number of elements in each element of the list it is not possible to use write() or write.table().

pms
  • 4,508
  • 4
  • 25
  • 30

8 Answers8

61

Not tested, but it should work (edited after comments)

lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000)
nico
  • 50,859
  • 17
  • 87
  • 112
  • 4
    Tested. Work. You could also use shorthand: `lapply(mylist, write, "test2.txt", append=TRUE)` – Marek Jun 15 '10 at 12:23
  • 1
    @Marek: Good! I didn't remember you could pass parameters to lapply like that! – nico Jun 15 '10 at 12:50
  • 5
    Just one important detail, you need to specify ncolumns parameter of write function, otherwise lines will be trimmed to 5 columns only: `lapply(mylist, write, "test3.txt", append=T, ncolumns=1000 )` – pms Jun 15 '10 at 13:30
  • 1
    Pls edit/add ncolumns to your answer and I will choose it as accepted answer! Shorthand is preferred :) – pms Jun 15 '10 at 13:37
  • 16
    Actually `write` is modified `cat` (see code) so alternative could be `lapply(mylist, cat, "\n", file="test.txt", append=TRUE)`. – Marek Jun 15 '10 at 19:36
  • 6
    @Marek each time I use this on my lists, it says 'Error in cat(list(...), file, sep, fill, labels, append):argument 1 (type 'list') cannot be handled by 'cat'' Unless I am reading this wrong, I've tried everyone's suggestion for my object of lists and none are working. – Kerry Oct 26 '12 at 11:59
  • 3
    @Kerry I could only guess but I suppose that your list is nested (list of lists). You can check it by `sapply(mylist, class)`, if there is value `"list"` that means you got nested list. – Marek Oct 26 '12 at 21:52
  • @marek so then how do I write that out? Basically, Im using MuMIn to do AIC selection and I want to print out the top models information and there doesn't seem to be a good way to get that out to a text file. – Kerry Nov 12 '12 at 17:46
  • @Kerry did you find a solution then? I have the same issue here. sapply(mylist,class) gives data.frame, which write should technically be able to handle, no? – FM Kerckhof Jan 23 '14 at 09:13
  • @FMKerckhof no, never did find a solution, ended up just copying and pasting by hand type thing. – Kerry Feb 17 '14 at 09:18
  • I also had the same issue. `mylist` is consist of `forecast` and `matrix`. – ali srn Jul 25 '16 at 08:27
  • This doesn't work propely and messes up the .txt every time, even with only numerics! – Squeezie Mar 14 '19 at 08:35
  • @nico. It's a really weird behaviour: I use this for a list of only numeric vectors and the text file lines get messed up at some point, even messing up the decimals – Squeezie Mar 14 '19 at 15:24
  • @Squeezie it's odd. I just tried the code on this: `mylist <- list(1:10, 11:15, 16:18)` and it works well. Maybe an OS-specific issue? I'm on Windows at the moment – nico Mar 14 '19 at 15:40
  • @Squeezie I am not quite sure... your code works fine for me. How exactly is the file "messed up"? – nico Mar 15 '19 at 13:06
  • @nico thanks for making sure the code works for me. Thats the file I get: https://ruhr-uni-bochum.sciebo.de/s/2ctlzBTFC2v6nde – Squeezie Mar 15 '19 at 14:36
  • 1
    @Squeezie I am not quite sure... I agree with you that it doesn't work... but I really cannot tell you why. Maybe you can try and open a new question. Sorry, but I am out of ideas... – nico Mar 15 '19 at 15:05
38

I saw in the comments for Nico's answer that some people were running into issues with saving lists that had lists within them. I also ran into this problem with some of my work and was hoping that someone found a better answer than what I found however no one responded to their issue.

So: @ali, @FMKerckhof, and @Kerry the only way that I found to save a nested list is to use sink() like user6585653 suggested (I tried to up vote his answer but could not). It is not the best way to do it since you link the text file which means it can be easily over written or other results may be saved within that file if you do not cancel the sink. See below for the code.

sink("mylist.txt")
print(mylist)
sink()

Make sure to have the sink() at the end your code so that you cancel the sink.

Jason
  • 381
  • 3
  • 2
  • 1
    great solution! the others got me an error this one works perfectly for my nested list. – agenis Apr 07 '17 at 15:24
  • 1
    +1 This solution also prints the names of the arguments of the list, which is something the accepted answer doesn't do. – mikey Nov 08 '21 at 19:50
15

Another way

writeLines(unlist(lapply(mylist, paste, collapse=" ")))
Marek
  • 49,472
  • 15
  • 99
  • 121
14

Here's another way using sink:

sink(sink_dir_and_file_name); print(yourList); sink()

user1981275
  • 13,002
  • 8
  • 72
  • 101
user6585653
  • 161
  • 1
  • 2
6

Format won't be completely the same, but it does write the data to a text file, and R will be able to reread it using dget when you want to retrieve it again as a list.

dput(mylist, "mylist.txt")
busybear
  • 10,194
  • 1
  • 25
  • 42
Danny Boy150
  • 61
  • 1
  • 1
3

depending on your tastes, an alternative to nico's answer:

d<-lapply(mylist, write, file=" ... ", append=T);
Carl
  • 7,538
  • 1
  • 40
  • 64
1

Here is another

cat(sapply(mylist, toString), file, sep="\n")
  • Thank you. This worked for me. With other solutions I received an error "...cannot be handled by 'cat''? Can I ask you what would be the solution if I have in the list two data frames and I would like to export theme into txt and keep the structure of the tables? Between them I would like to have an empty row. – Eco06 Jul 05 '16 at 10:56
-1

I solve this problem by mixing the solutions above.

sink("/Users/my/myTest.dat")
writeLines(unlist(lapply(k, paste, collapse=" ")))
sink()

I think it works well

kujungmul
  • 115
  • 1
  • 1
  • 7