Skip to content
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

unnest_longer with multiple cols - values_to=glue::glue({col}_long) not working #1555

Closed
werkstattcodes opened this issue Jun 24, 2024 · 1 comment

Comments

@werkstattcodes
Copy link

I am trying to unnest ("make longer") two columns at the same time ("in parallel").
The values of these columns should be unnested to two new columns.

The pertaining documentation reads as:

values_to: A string giving the column name (or names) to store the unnested values in. If multiple columns are specified in col, this can also be a glue string containing "{col}" to provide a template for the column names. The default, NULL, gives the output columns the same names as the input columns.

Adapting the example from the documentation site, I get however an error.
Many thanks!

library(tidyverse)

df <- tibble(
  x = 1:2,
  y = list(1:2, 3:4),
  z = list(5:6, 7:8)
)

df %>%
  unnest_longer(c(y, z), values_to=glue::glue("{col}_long"))
  
 Error: glue cannot interpolate functions into strings.
* object 'col' is a function.
@DavisVaughan
Copy link
Member

You don't need glue(), just pass the string

library(tidyverse)

df <- tibble(
  x = 1:2,
  y = list(1:2, 3:4),
  z = list(5:6, 7:8)
)

df %>%
  unnest_longer(c(y, z), values_to = "{col}_long")
#> # A tibble: 4 × 3
#>       x y_long z_long
#>   <int>  <int>  <int>
#> 1     1      1      5
#> 2     1      2      6
#> 3     2      3      7
#> 4     2      4      8

Created on 2024-10-24 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants