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

new version breaks pivot_wider #1318

Closed
dcaud opened this issue Feb 9, 2022 · 5 comments · Fixed by #1321
Closed

new version breaks pivot_wider #1318

dcaud opened this issue Feb 9, 2022 · 5 comments · Fixed by #1321

Comments

@dcaud
Copy link

dcaud commented Feb 9, 2022

Latest version of tidyr returns error about not being able to subset names that don't exist.

Reverting to ver. 1.1.4 makes the code work again.

I've no time to create a reprex, but here's essentially what breaks:

temp.pivot <- pivot_wider(
   output.df,
   id_cols = "boom",
   values_from = c(paste0(r,"some_suffix_name")), 
   names_from = "boom"
 )
@DavisVaughan
Copy link
Member

We are going to need more information to be able to help you. Please provide a reprex.

@DavisVaughan DavisVaughan added the reprex needs a minimal reproducible example label Feb 9, 2022
@dcaud
Copy link
Author

dcaud commented Feb 9, 2022

I'll try to provide a reprex. In the meantime, were there any breaking changes with the new version of tidyr?

@dcaud
Copy link
Author

dcaud commented Feb 9, 2022

Here's a reprex:

require(devtools)
install_version("tidyr", version = "1.1.4", repos = "http://cran.us.r-project.org")

This works:

tidyr::pivot_wider(
  iris,
  id_cols = "Species",
  values_from = Sepal.Length, 
  names_from = "Species"
)

restart R

require(devtools)
install_version("tidyr", version = "1.2.0", repos = "http://cran.us.r-project.org")

This gives an error

tidyr::pivot_wider(
  iris,
  id_cols = "Species",
  values_from = Sepal.Length, 
  names_from = "Species"
)

@tjmahr
Copy link
Contributor

tjmahr commented Feb 9, 2022

I had this same error. Basically, I had used a column name in both id_cols and in names_from. That worked in 1.1.3 but now fails in 1.2.0. I fixed my code by removing the column name from id_cols.

@DavisVaughan
Copy link
Member

Okay yea this was on purpose. Previously, when you duplicate a column name in both id_cols and names_from, it was getting used by names_from but silently dropped from the id_cols. This is almost always a typo on the user's part, you can't use the same name in both arguments.

So now id_cols is restricted to the subset of data after both names_from and values_from have been removed, which is why you get an error. This matches the default behavior if you don't supply id_cols at all, you get all the "remaining" columns as id_cols, so it makes sense that that should be all you have available to select from as well.

library(tidyr)

df <- tibble(
  names = c("x", "y"),
  value = c(1, 2)
)
df
#> # A tibble: 2 × 2
#>   names value
#>   <chr> <dbl>
#> 1 x         1
#> 2 y         2

# before
pivot_wider(
  df,
  id_cols = names, # <- this ends up getting ignored silently
  values_from = value, 
  names_from = names # <- this gets used
)
#> # A tibble: 1 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     2

# after
pivot_wider(
  df,
  id_cols = names, 
  values_from = value, 
  names_from = names
)
#> Error in `chr_as_locations()`:
#> ! Can't subset columns that don't exist.
#> x Column `names` doesn't exist.

I'll add an extra note to id_cols saying that if you supply a tidy selection, it is evaluated on data after removing names_from and values_from (it already mentions the default behavior, so this is just one step on top of that)

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

Successfully merging a pull request may close this issue.

3 participants