有什么方法可以按条件过滤R中的嵌套列表?

时间:2020-05-15 17:52:40

标签: r list filtering

我有一个嵌套列表,其中每个子列表都有不同的元素,我想按约束值阈值过滤此嵌套列表。由于存在嵌套列表,因此我对如何对此进行条件过滤不是很直观。基本上我想过滤掉子列表,其范围值在[0,100]之间。我找不到更好的方法来做到这一点。有什么办法可以在R中做到这一点吗?如果我将嵌套列表展平,则不容易进行条件过滤。谁能指出我如何做到这一点?有什么想法吗?

最小数据

dput(nestedList)
list(list(name = "ap6xl", value = NA, range_value = c(3.67907894466561, 
9.85654293641621, 26.4064566481452, 70.744982009271, 189.531391741786, 
507.769560968587, 1360.35473953304, 3644.49773995905, 9763.89715900568
), range_frequency = c(0.159044368600683, 0.0218430034129693, 
0.15221843003413, 0.238225255972696, 0.187030716723549, 0.154266211604096, 
0.0477815699658703, 0.0266211604095563, 0.0129692832764505), 
    importance = 0), list(name = "pct5", value = NA, range_value = c(2.00991466691426, 
5.9896040821492, 17.8491941232393, 53.1911168884407, 158.511073178148, 
472.367601770524, 1407.6691724348, 4194.8950173469, 12500.9089856849
), range_frequency = c(0, 0, 0.112016293279022, 0.190088255261371, 
0.280380176510523, 0.172437202987101, 0.173116089613035, 0.0454854039375424, 
0.0264765784114053), importance = 0), list(name = "crp4", value = NA, 
    range_value = c(6.40973678992026, 60.3946952687096, 569.059126161976, 
    5361.86642928566, 50521.3083909268, 476028.755134653, 4485302.99258328, 
    42262032.95132, 398207084.812742), range_frequency = c(0, 
    0, 0, 0, 0, 0.000670690811535882, 0.17102615694165, 0.379610999329309, 
    0.448692152917505), importance = 0), list(name = "age", value = NA, 
    range_value = c(22.3333333333333, 31, 39.6666666666667, 48.3333333333333, 
    57, 65.6666666666667, 74.3333333333333, 83, 91.6666666666667
    ), range_frequency = c(0.0288784419073203, 0.0577568838146407, 
    0.0591000671591672, 0.0826057756883815, 0.182001343183345, 
    0.22834116856951, 0.184016118200134, 0.159838817998657, 0.0174613834788449
    ), importance = 0), list(name = "temperature_value", value = NA, 
    range_value = c(86.7777777777778, 88.9333333333333, 91.0888888888889, 
    93.2444444444444, 95.4, 97.5555555555555, 99.7111111111111, 
    101.866666666667, 104.022222222222), range_frequency = c(0, 
    0, 0.000670241286863271, 0.00268096514745308, 0.00670241286863271, 
    0.524128686327078, 0.346514745308311, 0.0938337801608579, 
    0.0254691689008043), importance = 0), list(name = "heartrate_value", 
    value = NA, range_value = c(18.9444444444444, 36.8333333333333, 
    54.7222222222222, 72.6111111111111, 90.5, 108.388888888889, 
    126.277777777778, 144.166666666667, 162.055555555556), range_frequency = c(0, 
    0.0046916890080429, 0.0529490616621984, 0.213806970509383, 
    0.384048257372654, 0.235254691689008, 0.0878016085790885, 
    0.014745308310992, 0.00670241286863271), importance = 0), 
    list(name = "gcst_value", value = NA, range_value = c(3.2937197617506, 
    3.9386734842137, 4.70991764247814, 5.63218156768723, 6.73503691981872, 
    8.05384587946583, 9.63089500806102, 11.5167511328695, 13.7718827321278
    ), range_frequency = c(0, 0.00201342281879195, 0, 0.00134228187919463, 
    0.00268456375838926, 0.00134228187919463, 0.0140939597315436, 
    0.0161073825503356, 0.96241610738255), importance = 0))

我的尝试

这是我使用R代码的尝试:

filter_list <- function(range_interval, nested_list) {
    nested_list[lapply(nested_list, function(x) x$range_value) %in% range_interval]
}

但是这对我来说无法获得想要的输出。有什么主意吗?

所需输出

这是我想要获得的预期清单:

list(list(name = "gcst_value", value = NA, range_value = c(3.2937197617506, 
    3.9386734842137, 4.70991764247814, 5.63218156768723, 6.73503691981872, 
    8.05384587946583, 9.63089500806102, 11.5167511328695, 13.7718827321278
    ), range_frequency = c(0, 0.00201342281879195, 0, 0.00134228187919463, 
    0.00268456375838926, 0.00134228187919463, 0.0140939597315436, 
    0.0161073825503356, 0.96241610738255), importance = 0))

2 个答案:

答案 0 :(得分:2)

您可以在底数R中使用Filter

 Filter(function(x) all((x <- x$range_value) <= 100 & x >= 0), nestedList)

甚至:

 Filter(function(x) max(x <- x$range_value) <= 100 & min(x >= 0), nestedList)

答案 1 :(得分:1)

这是使用sapply的一种方式:

nestedList[sapply(nestedList,function(x){my.range <- range(x$range_value)
                                         my.range[1] >= 0 & my.range[2] <=100}
                  )]

这里是purrr的{​​{1}}方法:

keep
相关问题