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

Liaw, Andy andy_liaw at merck.com
Sat Feb 12 13:52:01 CET 2005


> From: Tiago R Magalhaes
> 
> 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

1. Please make sure the code you show actually works.  The last line
doesn't.

2. I'm not sure what you want to do.  A data frame is already a list.  If
you want it to be just a list, just use as.list(x).  If you want a list
where each component is a data frame with one column, use lapply(x,
as.data.frame).

Andy
 
> 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.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list