Reputation: 1769
I would like to write in a .txt
file the following list:
[[1]]
1 2 3 4 5 6 7 15
3430 145 29 6 1 3 2 1
[[2]]
1 2 3 4 5 13 22
3162 97 16 6 2 1 1
[[3]]
1 2 3 5 6 12
1659 83 15 2 2 1
as you can see above. I tried with
lapply(mylist, write, "test.txt", append=TRUE, ncolumns=1000)
but I obtained wrong result:
3430 145 29 6 1 3 2 1
3162 97 16 6 2 1 1
1659 83 15 2 2 1
(some of the data have been deleted).
Upvotes: 0
Views: 779
Reputation: 6483
lI assume you mean that in your example the names were not printed to the file? An easy but somewhat "hackish" way (but that achieves exactly the output as in your example) would be to use sink():
sink(file = "output.txt")
print(mylist)
sink(NULL)
Edit: Explanaiton
sink(file = "output.txt")
redirects (almost) all output in the following lines to the file specified
print(mylist)
prints normally to stdout, but since sink() is "active" its printed to the file (exactly the content that would otherwise printed to the console)
sink(NULL)
deactivates the redirect. See
?sink()
for additional information.
Upvotes: 1