Jim Bo
Jim Bo

Reputation: 657

Write list to a text file, preserving names, R

I want to write a list to a text file, preserving the names.

This is similar to R: Print list to a text file but with names which I want to print out also, at the start of each line:

> print(head(mylist,2))
$first
[1] 234984  10354  41175 932711 426928
$second
[1] 1693237   13462

mylist.txt
first   234984  10354  41175 932711 426928
second  1693237   13462

Any ideas?

Many thanks.

Upvotes: 10

Views: 14615

Answers (4)

Marco Stamazza
Marco Stamazza

Reputation: 916

@42-

To add to 42-'s answer (should have been a comment but then i couldn't format the code)

I needed to print also the names of the element's of the vectors in the list, so I added this line above the cat statement, as follows:

mylist <- list(first =c( a = 234984,  b = 10354,  c = 41175, d = 932711, e = 426928), 
           second =c( A = 1693237, B = 13462))


fnlist <- function(x, fil){ z <- deparse(substitute(x))
cat(z, "\n", file=fil)
nams=names(x) 
for (i in seq_along(x) ){ 
  cat("", "\t", paste(names(x[[i]]), "\t"), "\n", file=fil, append=TRUE)
  cat(nams[i], "\t",  x[[i]], "\n", file=fil, append=TRUE) }
}
fnlist(mylist, "test")

result

mylist 
         a      b     c      d     e     
first    234984 10354 41175 932711 426928 
         A       B   
second   1693237 13462 

Upvotes: 1

CJB
CJB

Reputation: 1809

If you are wanting to save the list for future use in R, then consider the rlist package.

put:

library("rlist")
list.save(mylist, "mylist.rds")

Then you can recover the list, including names, with:

mylist <- list.load("mylist.rds")

".rds" will not give a text file but will be completely recoverable. ".yaml" will give a text file, but some of the data structure will be lost upon reloading. ".json" almost gives best of both, although the text file might not be so readable and nested list structures may be simplified (e.g. list(1, 2, 3) will become a vector 1:3).

Upvotes: 2

James
James

Reputation: 66834

You can get a vector of the strings you require with:

sapply(names(mylist),function(x) paste(x,paste(mylist[[x]],collapse=" ")))
                                   first 
"first 234984 10354 41175 932711 426928" 
                                  second 
                  "second 1693237 13462"

Then you can write it with write or writeLines.

Upvotes: 6

IRTFM
IRTFM

Reputation: 263352

The cat function will print to a device (console by default) and not add any of the usual annotations, but it cannot accept a list as an argument, so everything needs to be an atomic vector. The deparse( substitute()) gambit is the way to recover names of lists that were passed to a function. Just using names(x) inside the function fails to recover the name of the original argument.

 mylist <- list(first =c( 234984,  10354,  41175, 932711, 426928), 
                second =c(1693237, 13462))
 fnlist <- function(x){ z <- deparse(substitute(x))
                         cat(z, "\n")
                         nams=names(x) 
                   for (i in seq_along(x) ) cat(nams[i],  x[[i]], "\n")}
 fnlist(mylist)
mylist 
second 234984 10354 41175 932711 426928 
first 1693237 13462 

This version would output a file (and you could substitute "\t" if you wanted tabs between names and values

fnlist <- function(x, fil){ z <- deparse(substitute(x))
                         cat(z, "\n", file=fil)
                         nams=names(x) 
                   for (i in seq_along(x) ){ cat(nams[i], "\t",  x[[i]], "\n", 
                                            file=fil, append=TRUE) }
                         }
 fnlist(mylist, "test")

Upvotes: 12

Related Questions