Open bpcolman opened 9 months ago
In my case, I could specify id_cols
like this and it worked. All indexes (col numbers) did not (i.e., when month_n
is specified as 8
)
pivot_wider(id_cols =
c(1:4, month_n),
names_from = "records_or_cards",
values_from = value)
Minimal reprex
library(tidyr)
# Set a seed for consistency
set_seed = 312
df <- data.frame(
site = c(rep((1:3), each = 2)),
time = rep(c("day", "night"), 3),
temp = rnorm(6, rep(c(20, 10), each = 1), 4),
day_of_exp = c(rep(1, 2), rep(11, 2), rep(21, 2))
)
# Columns 2 (time) and 3 (temp) are removed before evaluating the tidyselection
# for `id_cols`
df %>% pivot_wider(
id_cols = c(1, 4),
names_from = time,
values_from = temp
)
#> Error in `rethrow_id_cols_oob()`:
#> ! `i` must be a single string, not an integer vector.
#> ℹ This is an internal error that was detected in the tidyr package.
#> Please report it at <https://github.com/tidyverse/tidyr/issues> with a reprex
#> (<https://tidyverse.org/help/>) and the full backtrace.
Comes from:
~I think it is technically possible that we could evaluate the id_cols
tidyselection in the context of the full data frame, and then do a post-hoc check that it doesn't collide with any names specified by names_from
or values_from
(which is why we implement it this way in the first place).~ Actually this would not work, because then everything()
would also cause the error to get thrown, and we don't want that.
But I'd argue that using positions to perform the selection here is actually not a great thing to do. You should really prefer selecting with names. Ideally I think we'd keep the current behavior but prevent selecting by position through eval_select()
We've decided to wait for eval_select(allow_positional = FALSE)
and use that for id_cols
when we can to throw an informative error in this case and force the user to use names.
@DavisVaughan the reason I want to use positions is that it seems more parsimonious and for the lazy coder like me. However, using indices are more risky than specifying names.
In going through some old code that I hadn't touched in a while, I found that
pivot_wider
was not working and was giving me the following error:Digging deeper, I think I've sorted out the overarching timing of the issue, and it is due to changes made between
tidyr
1.1.4 and 1.2.0 that changes the behavior, and between versions 1.2.1 and 1.3.0 where the error message changed to the above. I've included a reprex below that shows behavior in ver 1.1.4 and 1.3.1:Created on 2024-02-15 with reprex v2.1.0
A workaround is to use the column names in the
id_cols
argument; however, given older versions of thepivot_wider
function allowed the use of column numbers and there is an increased potential for error when entering names, it would be nice ifpivot_wider
functioned like it did intidyr
1.1.4 and before. I would also advocate for a return to the more descriptive error message when choosing variables that don't exist if that is possible, and including a descriptive warning/error if one attempts to choose a variable that is included in thenames_from
orvalues_from
arguments.If the removal of straightforward selection by column number is a feature and not a bug, I would request an update for the help and vignette to make this even clearer.