-
Notifications
You must be signed in to change notification settings - Fork 415
ptype parameter of unnest() does not work #1594
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
Comments
It looks like it is working to me. You're asking it to convert each element of the original What type are you trying to preserve? If you can make a tibble of your expected outcome that would be helpful Were you going for something like this? library(tidyr)
df <- tibble(
id = 1:3,
test = list(1:2, "a", c(TRUE, FALSE)))
df %>%
dplyr::mutate(test = purrr::map(test, as.list)) %>%
unchop(test)
#> # A tibble: 5 × 2
#> id test
#> <int> <list>
#> 1 1 <int [1]>
#> 2 1 <int [1]>
#> 3 2 <chr [1]>
#> 4 3 <lgl [1]>
#> 5 3 <lgl [1]> Created on 2025-04-04 with reprex v2.1.1 |
Your solution is close to how I worked around this behaviour of ptype. My actual case is a bit more complicated, as I have tibbles within tibbles, but here what is happening with my data (data types now match my situation): library(tidyr)
library(dplyr)
nested_df1 <- tibble(
nested_id = 1:3,
test = list(c("a", "b", "c"), "f", c("d", "e")))
nested_df2 <- tibble(
nested_id = 4,
test = "f")
main_df <- tibble(
id = 1:2,
nested_dfs = list(nested_df1, nested_df2))
main_df %>% pull(nested_dfs)
#> [[1]]
#> # A tibble: 3 × 2
#> nested_id test
#> <int> <list>
#> 1 1 <chr [3]>
#> 2 2 <chr [1]>
#> 3 3 <chr [2]>
#>
#> [[2]]
#> # A tibble: 1 × 2
#> nested_id test
#> <dbl> <chr>
#> 1 4 f
main_df %>% unnest(nested_dfs, ptype = list(test = list()))
#> Error in `list_unchop()`:
#> ! Can't combine `x[[1]]$test` <list> and `x[[2]]$test` <character>. What I expected to happen is for Created on 2025-04-04 with reprex v2.1.1 |
I imagine what is confusing you is the fact that For you, So you'll have to manually convert to compatible types first, then combine library(tidyr)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
nested_df1 <- tibble(
nested_id = 1:3,
test = list(c("a", "b", "c"), "f", c("d", "e"))
)
nested_df2 <- tibble(
nested_id = 4,
test = "f"
)
main_df <- tibble(
id = 1:2,
nested_dfs = list(nested_df1, nested_df2)
)
main_df %>%
mutate(
nested_dfs = purrr::map(nested_dfs, function(df) {
if (!is.list(df$test)) {
df$test <- as.list(df$test)
}
df
})
) %>%
unnest(nested_dfs)
#> # A tibble: 4 × 3
#> id nested_id test
#> <int> <dbl> <list>
#> 1 1 1 <chr [3]>
#> 2 1 2 <chr [1]>
#> 3 1 3 <chr [2]>
#> 4 2 4 <chr [1]> Created on 2025-04-04 with reprex v2.1.1 |
Ah, yes, that makes sense. If ptype only applies after the unnesting had been done then yes, it won't work. So it's not a bug then :-) Thank you. |
Hello,
I've encountered a situation where I have to enforce type on specific columns during unnesting, and the parameter ptype does not seem to work:
Created on 2025-04-03 with reprex v2.1.1
The full error message is:
I believe this is a bug? The error persists with variants
ptype = list(test = as.character())
orptype = list(test = as.numeric())
.The text was updated successfully, but these errors were encountered: