Reputation: 191
I have 10000 bootstrap replicates of a zero-inflated GLMM I have run. These replicates are stored in a nested list with the original coefficients. A sample is provided below:
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"))))))
Many of the replicates failed to converge and need to be run again.
Question
How do I index the list without those elements with NaN? That is, how do I delete all elements with NaN from the list such that only elements $base_coef_se
, $resampled_coef_se[[3]]
, and $resampled_coef_se[[4]]
are retained?
Please note, I do not wish to delete only the NaN or the specific rows with a value that is NaN.
Upvotes: 0
Views: 144
Reputation: 389055
You can use Filter
in lapply
lapply(list, function(x) Filter(function(y) !any(sapply(y, is.na)), x))
Or using purrr
functions discard
and keep
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))))))
Upvotes: 2