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

Tiago R Magalhaes tiago17 at socrates.Berkeley.EDU
Sun Feb 13 21:06:49 CET 2005


thanks to Andy Liaw and james holtman that replied to my posting

this is very basic stuff, but for people strugling with these 
concepts - as I am... - here goes a more detailed explanation

#####
what I wanted was to create a list where every element would be a 
column of a given data.frame

the only small problem is that I cannot find a way to keep the column 
names - third paragraph of 3)

1)
x <- data.frame(a=sample(10), b=rep('a',10), c=sample(10))
xlistBycol <- as.list(x)

a data.frame is a list in which each column is a vector.
you can separate every vector/column of a database into elements of a 
list by using as.list
note that every element is a vector of the same type as in the data.frame
element 1 in xlistBycol is an integer, element 2 is a factor with 1 level ('a')

2)
x <- data.frame(a=sample(10), b=rep('a',10), c=sample(10))
xlistBycol <- lapply(x, as.data.frame)

the same concept as 1), but this time to each element/column of the 
data.frame, as.data.frame is applied
ther result of this call is a list in which every element is a 
data.frame with 1 column

if you want to keep the row.names you have to give row.names as an 
argument to as.data.frame:
xlistBycol <- lapply(x, as.data.frame, row.names(x))
Unfortunately so far I couldn't find an easy way to keep the column 
names of the data.frame in the corresponding elements of the list

3)
to better understand 2) it was useful for me to do this:
x <- data.frame(a=sample(10), b=sample(10))
xAddOne <- lapply(x, function(x) x <- x+1)

in this case lapply adds 1 to every element of the list/data.frame x 
and the result is the list xAddOne where every element is the 
corresponding element of x plus one

I hope this doesn't confuse people even more...




>  > 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




More information about the R-help mailing list