【问题标题】:R: Deleting all elements with NaN from a nested listR:从嵌套列表中删除所有带有 NaN 的元素
【发布时间】:2023-03-25 12:03:01
【问题描述】:

我已经运行了 10000 个零膨胀 GLMM 的引导程序副本。这些复制与原始系数一起存储在嵌套列表中。下面提供了一个示例:

dput(list)
list(base_coef_se = list(cond = structure(c(-1.09253995524795, 
0.0256301422371279, -1.0514969957993, 0.0240016805158904, 1.26641445088249, 
0.0802593307154245, 1.14126003018684, 0.146680139556809), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error"))), zi = structure(c(25.2015013459079, 
-1.21859733549644, 3.15690182258876, 1.19362141506194, 9.94706364916293, 
0.596529488331926, 1.43549374516962, 0.607416307342548), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error")))), resampled_coef_se = list(list(
cond = structure(c(34.9970753775844, -3.22572702513505, 17.6083767770097, 
3.37168668591985, NaN, NaN, NaN, NaN), .Dim = c(4L, 2L), .Dimnames = list(
    c("(Intercept)", "time", "post", "time_post"), c("Estimate", 
    "Std. Error"))), zi = structure(c(15.483714547828, -1.14317285084705, 
7.58752561192794, 1.20363485559809, NaN, NaN, NaN, NaN), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error")))), list(cond = structure(c(-146.774067639717, 
8.59851543039287, 38.8577512736417, -11.7226307624509, NaN, NaN, 
NaN, NaN), .Dim = c(4L, 2L), .Dimnames = list(c("(Intercept)", 
"time", "post", "time_post"), c("Estimate", "Std. Error"))), 
zi = structure(c(393.317183996712, -21.6891746824488, 222.051124261625, 
5.87025753585696, NaN, NaN, NaN, NaN), .Dim = c(4L, 2L), .Dimnames = list(
    c("(Intercept)", "time", "post", "time_post"), c("Estimate", 
    "Std. Error")))), list(cond = structure(c(42.2533620535011, 
-3.88611818057324, 22.4372206185289, 3.89030675595149, 29.2145236893512, 
2.65367313256613, 15.9311367067903, 2.65587575460308), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error"))), zi = structure(c(17.3647617397122, 
-1.34546016337903, 8.92430250655081, 1.36567882369832, 18.0255311579065, 
1.6716117456611, 10.5672462617773, 1.67614789060036), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error")))), list(cond = structure(c(-0.354897448197829, 
-0.0156398488462341, -1.04000607663322, 0.0754410074961221, 1.24513610433428, 
0.0787118964442171, 1.08966548000492, 0.140265121142117), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error"))), zi = structure(c(29.968034826095, 
-1.47628698180743, 2.96062386998035, 1.42736287027326, 12.6522852015015, 
0.758468697999968, 1.2969778116713, 0.762197527722121), .Dim = c(4L, 
2L), .Dimnames = list(c("(Intercept)", "time", "post", "time_post"
), c("Estimate", "Std. Error")))), list(cond = structure(c(-1.12374888354427, 
0.0274118172476898, 82.1775449647119, -83.6936071877345, NaN, 
NaN, NaN, NaN), .Dim = c(4L, 2L), .Dimnames = list(c("(Intercept)", 
"time", "post", "time_post"), c("Estimate", "Std. Error"))), 
zi = structure(c(25.6107080693792, -1.22260968583176, -86.6358416736526, 
91.338132285808, NaN, NaN, NaN, NaN), .Dim = c(4L, 2L), .Dimnames = list(
    c("(Intercept)", "time", "post", "time_post"), c("Estimate", 
    "Std. Error"))))))

许多复制未能收敛,需要再次运行。

问题

如何在没有 NaN 元素的情况下索引列表?也就是说,如何从列表中删除所有带有 NaN 的元素,从而只保留元素 $base_coef_se$resampled_coef_se[[3]]$resampled_coef_se[[4]]

请注意,我不希望 delete only the NaN 或值为 NaN 的特定行。

【问题讨论】:

    标签: r list


    【解决方案1】:

    你可以在lapply中使用Filter

    lapply(list, function(x) Filter(function(y) !any(sapply(y, is.na)), x))
    

    或者使用purrr函数discardkeep

    library(purrr)
    map(list, function(l) discard(l, function(l1) any(map_lgl(l1, ~any(is.na(.x))))))
    
    map(list, function(l) keep(l, function(l1) !any(map_lgl(l1, ~any(is.na(.x))))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2022-07-05
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      相关资源
      最近更新 更多