Skip to content

Commit 1e88616

Browse files
author
KANHONOU Lionel
committed
fix: unnest NA issues (mjskay#330) and (mjskay#332)
1 parent 1369c84 commit 1e88616

File tree

2 files changed

+122
-126
lines changed

2 files changed

+122
-126
lines changed

R/gather_draws.R

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,40 @@
1111
#' @importFrom rlang enquos
1212
#' @export
1313
gather_draws = function(
14-
model,
15-
...,
16-
regex = FALSE,
17-
sep = "[, ]",
18-
ndraws = NULL,
19-
seed = NULL,
20-
draw_indices = c(".chain", ".iteration", ".draw"),
21-
n
14+
model,
15+
...,
16+
regex = FALSE,
17+
sep = "[, ]",
18+
ndraws = NULL,
19+
seed = NULL,
20+
draw_indices = c(".chain", ".iteration", ".draw"),
21+
n
2222
) {
2323
ndraws = .Deprecated_argument_alias(ndraws, n)
24-
24+
2525
draws = sample_draws_from_model_(model, ndraws, seed)
26-
26+
2727
draw_indices = intersect(draw_indices, names(draws))
2828
tidysamples = lapply(enquos(...), function(variable_spec) {
29-
gather_variables(
29+
tidied = gather_variables(
3030
spread_draws_(draws, variable_spec, regex = regex, sep = sep, draw_indices = draw_indices),
3131
exclude = c(draw_indices, ".row")
3232
)
33+
34+
# Drop NAs for the specific variable columns after gathering
35+
tidied = tidied %>%
36+
filter(!is.na(.value)) # Filter out rows where .value is NA
37+
38+
return(tidied)
3339
})
34-
40+
3541
#get the groups from all the samples --- when we bind them together,
3642
#the grouping information is not always retained, so we'll have to recreate
3743
#the full set of groups from all the data frames after we bind them
3844
groups_ = tidysamples %>%
3945
lapply(group_vars) %>%
4046
reduce_(union)
41-
47+
4248
bind_rows(tidysamples) %>%
4349
group_by_at(groups_)
44-
}
50+
}

0 commit comments

Comments
 (0)