从R中列表中的矩阵中提取列

时间:2013-04-24 02:15:05

标签: r

我有100个元素的列表。每个元素都是一个列表。这个列表中有两个元素。一个是关键,另一个是矩阵。该矩阵中有5列。我试图从所有元素的每个矩阵中提取一列。所以基本上我会再将100个矩阵作为列表的一个元素。这就是列表的样子

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"

所以你在这里看到的是一个列表,它有100个元素。每个元素都是2个元素的列表。我目前只对第二个元素感兴趣。第二个元素有一个矩阵。我想提取这个矩阵的第一列。只是为了看看我是否可以将列提取到我可以做的向量中

a[[1]][[2]][,1] 

我将所有值作为向量,如下所示。

a[[1]][[2]][,1]  
       [1] "1329397986.572332" "1329242747.368294" "1329194224.351232" 

但是当我做的时候

 x <- lapply(a,function(r){r[[2]][,1]})

以下是我得到的错误。

> x <- lapply(a, function(r){r[[2]][,1]})
Error in r[[2]][, 1] : incorrect number of dimensions

我不知道为什么我会这样做。我检查了矩阵元素的尺寸。

> dim(a[[1]][[2]])
  [1] 52  5

看起来很正确。所以我不确定错误在哪里。

dput(a)给出

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"

2 个答案:

答案 0 :(得分:10)

对于很少使用的函数rapply来说,这似乎是一个完美的例子。

在发布可重现的示例之前,我将发布此作为可能的解决方案

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

答案 1 :(得分:4)

试试这个:

a <- list(list("keyid", structure(c("1329397986.572332", "1329242747.368294", 
"1329194224.351232", "x", "x", "x", "y", "y", "y", "z", "z", 
"z", "a", "a", "a"), .Dim = c(3L, 5L))), list("keyid", structure(c("1329397986.572332", 
"1329242747.368294", "1329194224.351232", "x", "x", "x", "y", 
"y", "y", "z", "z", "z", "a", "a", "a"), .Dim = c(3L, 5L))))

sapply( lapply(a, "[[" , 2) , "[", TRUE, 1)
     [,1]                [,2]               
[1,] "1329397986.572332" "1329397986.572332"
[2,] "1329242747.368294" "1329242747.368294"
[3,] "1329194224.351232" "1329194224.351232"

说明:内部lapply拉出每个子列表中的第二个元素,并将其传递给外部sapply。 (外部)sapply使用[从传递给它的每个对象中拉出第一列。由于[是原始函数,因此.i.e。只使用位置参数的一个,你需要使用占位符,这是提供“TRUE”参数的目的。它的目的是将“1”“推”到第二个参数位置,从而使它成为列选择器而不是行选择器。