-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FR: New keep_empty = FALSE
argument to list_c()
and list_rbind()
#1096
Comments
Hmmmm, this doesn't feel quite right for purrr, but maybe it's the least worst place? |
keep_empty = FALSE
argument to list_c()
keep_empty = FALSE
argument to list_c()
and list_rbind()
I think the way to implement this for both |
The original use case was a data set with a column with varying-type data. library(tidyverse)
variant_data <- tibble(
key = c(1, 1, 2, 2),
name = letters[c(1:2, 1:2)],
value = list("a", 1, NULL, NULL)
)
variant_data |>
pivot_wider()
#> # A tibble: 2 × 3
#> key a b
#> <dbl> <list> <list>
#> 1 1 <chr [1]> <dbl [1]>
#> 2 2 <NULL> <NULL>
# How to achieve this output:
tibble(
key = c(1, 2),
a = c("a", NA),
b = c(1, NA),
)
#> # A tibble: 2 × 3
#> key a b
#> <dbl> <chr> <dbl>
#> 1 1 a 1
#> 2 2 <NA> NA Created on 2024-07-16 with reprex v2.1.0 |
I've come back to look at @krlmlr's original example. It's just library(tidyverse)
variant_data <- tibble(
key = c(1, 1, 2, 2),
name = letters[c(1:2, 1:2)],
value = list("a", 1, NULL, NULL)
)
variant_data |>
pivot_wider() |>
unchop(c(a, b), keep_empty = TRUE)
#> # A tibble: 2 × 3
#> key a b
#> <dbl> <chr> <dbl>
#> 1 1 a 1
#> 2 2 <NA> NA So now I'm questioning if The other thing that is nice about variant_data |>
pivot_wider()
#> # A tibble: 2 × 3
#> key a b
#> <dbl> <list> <list>
#> 1 1 <chr [1]> <dbl [1]>
#> 2 2 <NULL> <NULL> If those list elements are size |
See #1144 (comment) |
I'm looking for the following behavior so that I can use
pivot_wider() %>% mutate(across(..., ~ list_c(.x, keep_empty = TRUE)))
as a more robust replacement fortidyr::spread(convert = TRUE)
.I can achieve it today with the lower-level
list_unchop()
, in a very clumsy way.Created on 2023-08-20 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: