I have encountered the following problem: I need to extract from a list of lists equally named compenents who happen to be 'one row' data frames. a trivial example would be: a <- list(list( df = data.frame(A = 1, B = 2, C = 3)), list(df = data.frame(A = 4,B = 5,C = 6))) I want the extracted compenents to fill up a matrix or data frame row by row. the obvious thing to do seems: b <- sapply(a, "[[", "df") b <- t(b) now `b' looks all right: b class(b) but it turns out that all elements in this matrix are one element lists: class(b[1,1]) which prevents any further standard processing of `b' (like `colMeans', e.g.) question 1: is their a straightforward way to enforce that `b' contains simple numbers as elements right from the start (instead of something like apply(b, 1:2, "class<-", "numeric") afterwards)? question 2: should not sapply do this further 'simplification' anyway in a situation like this (matrix elements turn out to be one-element lists)? regards joerg
sapply problem
5 messages · Joerg van den Hoff, Weiwei Shi, Sundar Dorai-Raj +1 more
c <- apply(b, c(1,2), unlist) c
A B C [1,] 1 2 3 [2,] 4 5 6
class(c[1,1])
[1] "numeric"
On 12/14/06, Joerg van den Hoff <j.van_den_hoff at fz-rossendorf.de> wrote:
I have encountered the following problem: I need to extract from a list of lists equally named compenents who happen to be 'one row' data frames. a trivial example would be: a <- list(list( df = data.frame(A = 1, B = 2, C = 3)), list(df = data.frame(A = 4,B = 5,C = 6))) I want the extracted compenents to fill up a matrix or data frame row by row. the obvious thing to do seems: b <- sapply(a, "[[", "df") b <- t(b) now `b' looks all right: b class(b) but it turns out that all elements in this matrix are one element lists: class(b[1,1]) which prevents any further standard processing of `b' (like `colMeans', e.g.) question 1: is their a straightforward way to enforce that `b' contains simple numbers as elements right from the start (instead of something like apply(b, 1:2, "class<-", "numeric") afterwards)? question 2: should not sapply do this further 'simplification' anyway in a situation like this (matrix elements turn out to be one-element lists)? regards joerg
______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III
Joerg van den Hoff said the following on 12/14/2006 7:30 AM:
I have encountered the following problem: I need to extract from a list of lists equally named compenents who happen to be 'one row' data frames. a trivial example would be: a <- list(list( df = data.frame(A = 1, B = 2, C = 3)), list(df = data.frame(A = 4,B = 5,C = 6))) I want the extracted compenents to fill up a matrix or data frame row by row. the obvious thing to do seems: b <- sapply(a, "[[", "df") b <- t(b) now `b' looks all right: b class(b) but it turns out that all elements in this matrix are one element lists: class(b[1,1]) which prevents any further standard processing of `b' (like `colMeans', e.g.) question 1: is their a straightforward way to enforce that `b' contains simple numbers as elements right from the start (instead of something like apply(b, 1:2, "class<-", "numeric") afterwards)?
Try this:
a <- list(list(df = data.frame(A = 1, B = 2, C = 3)),
list(df = data.frame(A = 4, B = 5, C = 6)))
b <- do.call("rbind", sapply(a, "[", "df"))
b
question 2: should not sapply do this further 'simplification' anyway in a situation like this (matrix elements turn out to be one-element lists)?
I think it does as it much as it knows how. I think you might believe
that matrix elements can only contain numeric values. This is not a
valid assumption. Take this example:
> a <- list(1)
> b <- list(2)
> (m <- matrix(c(a, b), 2, 1))
[,1]
[1,] 1
[2,] 2
> class(m[1, 1])
[1] "list"
> class(m[2, 1])
[1] "list"
HTH,
--sundar
regards joerg
______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061214/48783f7b/attachment.pl
Sundar Dorai-Raj wrote:
Joerg van den Hoff said the following on 12/14/2006 7:30 AM:
I have encountered the following problem: I need to extract from a list of lists equally named compenents who happen to be 'one row' data frames. a trivial example would be: a <- list(list( df = data.frame(A = 1, B = 2, C = 3)), list(df = data.frame(A = 4,B = 5,C = 6))) I want the extracted compenents to fill up a matrix or data frame row by row. the obvious thing to do seems: b <- sapply(a, "[[", "df") b <- t(b) now `b' looks all right: b class(b) but it turns out that all elements in this matrix are one element lists: class(b[1,1]) which prevents any further standard processing of `b' (like `colMeans', e.g.) question 1: is their a straightforward way to enforce that `b' contains simple numbers as elements right from the start (instead of something like apply(b, 1:2, "class<-", "numeric") afterwards)?
Try this:
a <- list(list(df = data.frame(A = 1, B = 2, C = 3)),
list(df = data.frame(A = 4, B = 5, C = 6)))
b <- do.call("rbind", sapply(a, "[", "df"))
b
question 2: should not sapply do this further 'simplification' anyway in a situation like this (matrix elements turn out to be one-element lists)?
I think it does as it much as it knows how. I think you might believe that matrix elements can only contain numeric values. This is not a valid assumption. Take this example:
> a <- list(1) > b <- list(2) > (m <- matrix(c(a, b), 2, 1))
[,1] [1,] 1 [2,] 2
> class(m[1, 1])
[1] "list"
> class(m[2, 1])
[1] "list" HTH, --sundar
regards joerg
thanks for the help to everybody. the proposal of sundar seems the most concise one (if it is also efficient I'll see with larger data sets ..). with regards to my question no. 2: sure I realize that I _can_ have a sensible matrix consisting of elements that are lists. with regards to `sapply' and the intention (as I understand it) to simplify as much as is sensible possible (`sapply' unlists usually everything it can), I think it would be desirable if `sapply' would detect this admittedly very special case: "a matrix of one-element lists whose sole list elements are atomic of length 1" (which was my case and is exactly your example above). of course, I don't know whether such a special treatment (probably meaning further recursive tests in `sapply') would slow down `sapply' significantly in general (which would be an argument against such a change of `sapply') or maybe it is against the actual purpose of sapply -- I'm not sure. anyway, thanks again, joerg