Closed Darxor closed 10 months ago
Interesting - I wasn't aware of this functionality.
I can tackle this if you want, though this may lead to a rather big change in how
pivot_wider()
is coded
Yeah if you want to take a shot at this feel free to.
I think this is the general approach we need. But let me know if I'm missing anything.
Also - this is sort of pseudo-code below, I don't know if this is exactly how it will work.
Once unused_cols
are identified there are two parts:
1) If is.null(unused_fn)
the columns are dropped from the data frame pre-pivoting (relatively straightforward)
2) If !is.null(unused_fn)
we need to aggregate the unused_cols
with the unused_fn
For part 2 - wouldn't we basically just need something like this?
(Probably with a conditional if statement)
unused_df <- select(.df, all_of(id_cols), all_of(unused_cols))
unused_df <- summarize(unused_df, across(all_of(unused_cols), unused_fn), .by = all_of(id_cols))
out <- bind_cols(out, unused_df)
# And maybe a relocate step to have the correct column order? Or maybe select?
# out <- select(out, all_of(id_cols), all_of(unused_cols), everything())
Interesting - I wasn't aware of this functionality.
I think my approach with tidyverse sometimes relies on a lesser-known functionality, as you can see by my issues, haha.
I will look into this, yeah! Sounds about right with that approach, I will have to think about how to identify id_cols
and unused_cols
correctly (order of operation concerns me a bit). I've ran some tests from {tidyr}
that seem related to this, and currently they are not passed.
BTW, I think its also a good idea to port some tests over from tidyverse and look for missing / non-matching / broken functionality between packages.
All set - sorry this one took so long to get to!
library(tidytable)
df <- data.frame(
a = LETTERS[1:2],
b = LETTERS[3:4],
val = 1:2
)
df %>%
pivot_wider(
id_cols = character(0),
names_from = a,
values_from = val
)
#> # A tidytable: 1 × 2
#> A B
#> <int> <int>
#> 1 1 2
Currently specifying
character(0)
/numeric(0)
inid_cols
leads to behavior similar toNULL
(the default), but also produces a warning:Created on 2022-11-29 with reprex v2.0.2
{tidyr}
handles it differently. They apply function passed in the argumentunused_fn
(default is to omit columns) to all columns not mentioned inid_cols
,names_from
, orvalues_from
, which leads to this:Created on 2022-11-29 with reprex v2.0.2
Another note: with
NULL
inid_cols
,{tidyr}
considers all columns not mentioned bynames_from
, orvalues_from
asid_cols
, which affectsid_expand
(currently also not implemented, but a thing to keep in mind for future).I can tackle this if you want, though this may lead to a rather big change in how
pivot_wider()
is coded 😅