Tiago R Magalhaes
2005-Feb-12 08:17 UTC
[R] data.frame into list by columns; merge and row.names
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 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.
> 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 appreciate1. 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 > >
Gabor Grothendieck
2005-Feb-12 14:16 UTC
[R] data.frame into list by columns; merge and row.names
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.
Tiago R Magalhaes
2005-Feb-13 20:06 UTC
[R] data.frame into list by columns; merge and row.names
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