extract a column from a matrix in a list in R
r
Solution
This seems a perfect case for the little-used function `rapply`.
Until a reproducible example is posted, I will post this as a possible solution
set.seed(1)
# a possible reproducible example
a <- replicate(5, list('a', matrix(runif(10), ncol=5)), simplify = FALSE)
# the first column
a1 <- rapply(a, classes = 'matrix', how = 'list', f = function(x) x[, 1, drop = FALSE])
# a second `lapply` is required to drop `NULL` entries
a1Only <- lapply(a1, Filter, f = Negate(is.null))
Problem
I have a list of 100 elements. Each element is a list. And this list has two elements in them. One is just a key another is a matrix. This matrix has 5 columns in it. I am trying to extract one column from each of these matrices for all the elements. So basically I would have 100 matrices again as an element of the list. This is how the list looks like ``` a[[1]] [[1]] [1] "keyid" [[2]] [,1] [,2] [,3] [,4] [,5] [1,] "1329397986.572332" "x" "y" "z" "a" [2,] "1329242747.368294" "x" "y" "z" "a" [3,] "1329194224.351232 "x" "y" "z" "a" ``` So what you can see here is a is a list and it has 100 elements. Each element is a list of 2 elements. I am currently interested only in the second element. The second element has a matrix. I would like to extract the first column of this matrix. Just to see if I can extract the column into a vector I can do ``` a[[1]][[2]][,1] ``` and I get all the values as a vector, as follows. ``` a[[1]][[2]][,1] [1] "1329397986.572332" "1329242747.368294" "1329194224.351232" ``` But when I do ``` x <- lapply(a,function(r){r[[2]][,1]}) ``` the following is the error I get. ``` > x <- lapply(a, function(r){r[[2]][,1]}) Error in r[[2]][, 1] : incorrect number of dimensions ``` I am not sure why I am getting that. I checked the dimensions of the matrix element. ``` > dim(a[[1]][[2]]) [1] 52 5 ``` and it looks correct. So I am not sure where the error is. dput(a) gives ``` dput(a) [[1]] [[1]][[1]] [1] "keyid" [[1]][[2]] [,1] [,2] [,3] [,4] [,5] [1,] "1329397986.572332" "x" "y" "z" "a" [2,] "1329242747.368294" "x" "y" "z" "a" [3,] "1329194224.351232" "x" "y" "z" "a" [[2]] [[2]][[1]] [1] "keyid" [[2]][[2]] [,1] [,2] [,3] [,4] [,5] [1,] "1329397986.572332" "x" "y" "z" "a" [2,] "1329242747.368294" "x" "y" "z" "a" [3,] "1329194224.351232" "x" "y" "z" "a" ... [[100]] [[100]][[1]] [1] "keyid" [[100]][[2]] [,1] [,2] [,3] [,4] [,5] [1,] "1329397986.572332" "x" "y" "z" "a" [2,] "1329242747.368294" "x" "y" "z" "a" [3,] "1329194224.351232" "x" "y" "z" "a" ```