R从嵌套列表的密度中提取范围

时间:2014-09-19 08:36:39

标签: r

我有一个嵌套列表density_subset_list

它包含6个列表,每个列表包含另外3个密度数据列表。 e.g。

dsl <- A(all_density, p1_density,p2_density), B(all_density, p1_density,p2_density)

我想要整体y范围。

这是我的尝试。

for (i in 1:length(INTlist)){
  y <- unlist(lapply(density_subset_list[[i]], function(d) range(d$y)))
  yall <- c(y, yall)
}

range(yall)

它似乎不起作用。 任何帮助表示赞赏 感谢

str(density_subset_list)
  List of 6
      $ STRexp :List of 3
    ..$ all:List of 7
    .. ..$ x        : num [1:512] -0.712 -0.708 -0.705 -0.702 -0.698 ...
    .. ..$ y        : num [1:512] 2.17e-14 3.64e-14 5.99e-14 9.64e-14 1.62e-13 ...
    .. ..$ bw       : num 0.047
    .. ..$ n        : int 1127
    .. ..$ call     : language density.default(x = x$corr, from = min(Sa14_scoreCorr$corr), to = max(Sa14_scoreCorr$corr),      na.rm = T)
    .. ..$ data.name: chr "x$corr"
    .. ..$ has.na   : logi FALSE
    .. ..- attr(*, "class")= chr "density"
    ..$ Kan:List of 7
    .. ..$ x        : num [1:512] -0.712 -0.708 -0.705 -0.702 -0.698 ...
    .. ..$ y        : num [1:512] 2.60e-08 3.42e-08 4.50e-08 5.88e-08 7.62e-08 ...
    .. ..$ bw       : num 0.0649
    .. ..$ n        : int 287
    .. ..$ call     : language density.default(x = x$corr, from = min(Sa14_scoreCorr$corr), to = max(Sa14_scoreCorr$corr),      na.rm = T)
    .. ..$ data.name: chr "x$corr"
    .. ..$ has.na   : logi FALSE
    .. ..- attr(*, "class")= chr "density"
    ..$ Cm :List of 7
    .. ..$ x        : num [1:512] -0.712 -0.708 -0.705 -0.702 -0.698 ...
    .. ..$ y        : num [1:512] 3.88e-08 4.79e-08 5.94e-08 7.38e-08 9.10e-08 ...re

1 个答案:

答案 0 :(得分:0)

你非常接近,但不需要for

set.seed(100)

dat <- list(STRexp=list(list(), list(), list()), 
            all=list(y=sample(100, 50)), 
            Kan=list(y=sample(10,3)), 
            Cm=list(y=sample(1000, 100)))
str(dat)

## List of 4
##  $ STRexp:List of 3
##   ..$ : list()
##   ..$ : list()
##   ..$ : list()
##  $ all   :List of 1
##   ..$ y: int [1:50] 31 26 55 6 45 46 77 35 51 16 ...
##  $ Kan   :List of 1
##   ..$ y: int [1:3] 4 2 9
##  $ Cm    :List of 1
##   ..$ y: int [1:100] 275 591 253 124 229 595 211 461 642 952 ...


# this will get the range of each "y" then get the overall range 

range(unlist(lapply(names(dat)[-1], function(x) range(dat[[x]]$y))))

## [1]   2 991