从索引列表中的所有对象中提取组件

时间:2015-03-30 01:41:15

标签: r

我有一个包含多个对象的索引列表,每个对象包含3个矩阵($ tab,$ nobs和$ other)。列表中有数百个这样的对象。目标是只访问$ tab矩阵并从每个对象转置它。

genfreqT <- lapply(genfreq[[1:100]]$tab, function(x) t(x))

这似乎不起作用。

以下是genfreq对象的结构。这是用R package adegenet创建的。

> str(genfreq[[1]])
List of 3
 $ tab : num [1:30, 1:1974] 0.6 0.5 0.325 0.675 0.6 0.5 0.5 0.375 0.55 0.475 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : Named chr [1:30] "1" "2" "3" "4" ...
  .. .. ..- attr(*, "names")= chr [1:30] "01" "02" "03" "04" ...
  .. ..$ : chr [1:1974] "L0001.1" "L0001.2" "L0002.1" "L0002.2" ...
 $ nobs: num [1:30, 1:1000] 40 40 40 40 40 40 40 40 40 40 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : Named chr [1:30] "1" "2" "3" "4" ...
  .. .. ..- attr(*, "names")= chr [1:30] "01" "02" "03" "04" ...
  .. ..$ : Named chr [1:1000] "L0001" "L0002" "L0003" "L0004" ...
  .. .. ..- attr(*, "names")= chr [1:1000] "L0001" "L0002" "L0003" "L0004" ...
 $ call: language makefreq(x = x, truenames = TRUE)

2 个答案:

答案 0 :(得分:2)

genfreqT <-lapply(lapply(genfreq, "[[", "tab"),function(x) t(x))

答案 1 :(得分:0)

&#39; Adegenet&#39;提供了这个解决方案:

> genfreqT <- lapply(genfreq, function(e) t(e$tab))

> summary(genfreqT)
                  Length Class  Mode   
data1.str 59220  -none- numeric
data2.str 59220  -none- numeric
data3.str 59220  -none- numeric