简体   繁体   中英

How to save data a list to a text file in R?

I have an R object which is actually a list

> str(snpset)
List of 16
$ chr1 : int [1:80] 2 6 7 8 11 13 16 17 18 21 ...
$ chr2 : int [1:78] 146 148 150 151 152 153 155 156 158 159 ...
$ chr3 : int [1:55] 276 279 281 284 289 295 298 301 304 306 ...
$ chr4 : int [1:66] 404 406 407 408 410 411 412 414 415 416 ...
$ chr5 : int [1:42] 505 506 508 509 511 514 515 516 518 521 ...
$ chr6 : int [1:38] 589 591 592 593 594 595 600 601 603 604 ...
$ chr7 : int [1:78] 660 663 664 665 666 671 673 677 679 681 ...
$ chr8 : int [1:69] 846 847 849 851 854 855 858 862 863 864 ...
$ chr9 : int [1:48] 968 969 970 971 972 974 980 981 982 983 ...
$ chr10: int [1:49] 1063 1066 1068 1069 1071 1072 1076 1077 1082 1083 ...
$ chr11: int [1:84] 1159 1160 1161 1162 1163 1164 1166 1169 1170 1172 ...
$ chr12: int [1:81] 1311 1313 1314 1315 1317 1318 1321 1323 1324 1326 ...
$ chr13: int [1:51] 1460 1461 1462 1464 1465 1466 1468 1469 1471 1472 ...
$ chr14: int [1:47] 1544 1546 1547 1548 1551 1552 1557 1559 1563 1564 ...
$ chr15: int [1:55] 1650 1651 1653 1654 1655 1657 1658 1659 1660 1661 ...
$ chr16: int [1:37] 1756 1757 1759 1761 1762 1763 1764 1767 1769 1770 ...

How can I save all the data in the object,"snpset" as a text file?

I tried the following:

write.table(snpset,file="test",sep=" ", row.names=TRUE)

But got this error:

Error in data.frame(chr1 = c(2L, 6L, 7L, 8L, 11L, 13L, 16L, 17L, 18L, :
 arguments imply differing number of rows: 80, 78, 55, 66, 42, 38, 69, 48,
 49, 84, 81, 51, 47, 37 –

write.csv (and the whole family of write. functions) accepts data.frames and matrices, and objects that can be converted to those types of structures. Lists are not part of them (because they're not rectangular would be a short a simple answer) -- hence the error message. Depending on what you want to do, here are two solutions (among many other possible ones).

  1. If you just need the data per se, separated by space, with or without row numbers, cat is enough:

     for(i in seq_along(snpset)) cat(paste0(i,"."), snpset[[i]], "\\n", file = "somefile.txt", append = TRUE) 

    If you don't need line numbers just remove paste0(i,"."),

    File content (using random data)

     1. 64 44 31 70 78 99 100 88 79 58 2. 43 155 105 151 190 177 192 108 168 110 3. 204 227 131 51 113 261 67 187 272 100 131 78 45 4. 45 259 400 64 127 70 186 213 197 101 220 5. 404 396 89 215 421 262 457 64 184 150 360 165 481 498 378 (...) 
  2. To have a textual representation of the whole object that you can easily re-read into R, dput is a good choice:

     dput(snpset, "someOtherFile.txt") 

    Then to get it back into R you would do

     snpset <- source("someOtherFile.txt")$value 

    File Content

     list(c(64L, 44L, 31L, 70L, 78L, 99L, 100L, 88L, 79L, 58L), c(43L, 155L, 105L, 151L, 190L, 177L, 192L, 108L, 168L, 110L), c(204L, 227L, 131L, 51L, 113L, 261L, 67L, 187L, 272L, 100L, 131L, 78L, (...) 

Generated data

snpset <- list()
for(i in 1:10) {
    snpset[[i]] <- sample(c((i*10):(i*100)),size = sample(10:15, size = 1), replace=TRUE)
}

Here is an easier one-line solution:

sapply(snpset, function(x) {write(paste(x, collapse=', '), file="output.txt",append=TRUE)})

Or if you want to make it easier to read:

sapply(snpset, function(x) {
                   write(paste(x, collapse=', '),
                   file="output.txt",append=TRUE)
               })

output.txt :

2, 6, 7, 8, 11, 13, 16, 17, 18, 21, ...
146, 148, 150, 151, 152, 153, 155, 156, 158, 159, ...
276, 279, 281, 284, 289, 295, 298, 301, 304, 306, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM