簡體   English   中英

R從嵌套列表的密度中提取范圍

[英]R extracting range from density of nested lists

我有一個嵌套的列表density_subset_list

它包含6個列表,每個列表又包含另外3個密度數據列表。 例如

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

您距離很近,但是不需要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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM