-
Notifications
You must be signed in to change notification settings - Fork 421
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
pivot_wider: unexpected behavior specifying id_cols to omit that are included in names/values_from #1506
Comments
Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session. Thanks! |
@hadley I admit I haven't used this before, so hopefully the below is the RightWay. I don't see much advantage other than the setup code and the failing code aren't separated by one line of my dialog. Here's a reprex for the case that surprised me:
|
@jwhendy it doesn't look like you copied and pasted it correctly. And it does make my life easier having all the code in one block, because there's just one thing to copy and paste, rather than having to stitch together multiple pieces. You can also remove the call to dplyr, because that doesn't seem necessary. library(tidyr)
tmp <- data.frame(
id1 = c("a", "a", "b", "b"),
unused = c(NA, NA, NA, NA),
type = c("c", "d", "c", "d"),
values = c(1, 2, 3, 4)
)
tmp |> pivot_wider(id_cols = -c(type, values, unused), names_from = type, values_from = values)
#> Error in `pivot_wider()`:
#> ! `id_cols` can't select a column already selected by `names_from`.
#> ℹ Column `type` has already been selected. Created on 2023-11-01 with reprex v2.0.2 The error message doesn't seem correct because you're not actually selecting |
Understood, and don't want to be a hassle! Boy, the docs are
I selected this, then copied to clip board.
So I ran
Coming back here to paste: tmp %>% pivot_wider(id_cols = -c(type, values, unused), names_from = type, values_from = values)
#> Error in tmp %>% pivot_wider(id_cols = -c(type, values, unused), names_from = type, : could not find function "%>%"
library(tidyr)
tmp <- data.frame(id1 = c("a", "a", "b", "b"),
unused = c(NA, NA, NA, NA),
type = c("c", "d", "c", "d"),
values = c(1, 2, 3, 4))
tmp %>% pivot_wider(id_cols = -c(type, values, unused), names_from = type, values_from = values)
#> Error in `pivot_wider()`:
#> ! `id_cols` can't select a column already selected by `names_from`.
#> ℹ Column `type` has already been selected.
#> Backtrace:
#> ▆
#> 1. ├─tmp %>% ...
### snipped for brevity, but same as above
#> 41. └─rlang::abort(...)
<sup>Created on 2023-11-01 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup> At least the output seems reproducible 2x in a row :)
My bad. I thought
That was my thinking, though I regularly consider myself in noob territory despite having used R a long time! At the least, I thought the message could clarify why this is problematic. The example seems trivial, but at the time, there were a bunch of columns, so I'd much rather Thanks for taking a look! |
Do you have the latest version of reprex? And how are you running R? (e.g. in RStudio on your desktop?) |
Would you like me to create a ticket in the reprex repo? I just installed it yesterday. After installing, I wasn't sure if any loaded environment objects would goof things up, so my process was:
|
@jwhendy yes please! |
@hadley I'm delayed, but tis done. Thanks! |
I was running
pivot_wider
on some data and was surprised by the inability to use-c(col1, col2)
to choose myid_cols
, resulting in the error:Repro:
Base case:
But say you had a lot of columns; it's more concise to remove a few than name them all. Neither of these work, and produce the error above:
My failure mode may be covered by this statement from the docs:
This is why I included the "unused" column, as for data with many columns, one would have to think about "ok, I'm removing
type
andvalues
'for free' since they are used in other args, but I do need to remember to remove those other columns."Thoughts:
names_from
orvalues_from
type
has already been selected"... by what/how?" It was non-intuitive to me that it's "selected" when I'm trying to explicitly not select it as an id_colThe text was updated successfully, but these errors were encountered: