[R] data.frame into list by columns; merge and row.names

Gabor Grothendieck ggrothendieck at myway.com
Sat Feb 12 15:16:12 CET 2005


Tiago R Magalhaes <tiago17 <at> socrates.Berkeley.EDU> writes:

: 
: Hi
: 
: a)
: I want to make a list out of a data.frame, where each element of the 
: list is a column of the data.frame.
: I looked in the archives and saw a lot of postings but surprsingly 
: none elucidated me. I also tried the split, aggregate help files and 
: counldn't see any easy way to do this. I wouldn't be surprised if 
: it's there, but I really didn't see it.
: 
: I solved the problem using a very convoluted way:
: 
: x <- data.frame(a=sample(10), b=sample(10), c=sample(10))
: f <- factor(names(x), levels=names(x))
: xx <- data.frame(f=f, t(x))
: xlist.transpose <- split(xx, xx$f)
: xlist <- lapply(xlist, function(x) x=t(x))
: 
: I am very convinced there's a much easier way, so if any of you 
: people enlighten me I would appreciate

A data frame _is_ a list.  You could do this:

class(x) <- "list"  # x is from above

but you may not even need to do that and may be able to use
x directly depending on what you need to do next.

: 
: b)
: In terms of my own personal use, it would be much better that merge 
: when using 'row.names' as the by argument would output a data.frame 
: with the merged row.names and not a column 'Row.names'.
: 
: Also it would be great if it would be possible to choose wich sorting 
: would be the final one - right now the default is argument y, but 
: sometimes argument x is much more useful
: Of course these are minor points and can be dealt very easily after 
: calling merge, but here it goes my comment anyway.




More information about the R-help mailing list