dplyr
dplyr copied to clipboard
More examples in `across()` documentation
When I use across() I almost always want to do the thing to every single column. For whatever reason, that tidyselect action is hard for me to remember/find. Is it all()? Is it everything()? Etc. It would be great to have an example in the across() documentation that does the thing to every column. Additionally, it would be awesome to have an example that does the thing conditionally depending on the variable type. For example, rounding all the numeric variables.
I second this. Honestly I find across() super confusing and frequently revert to the _if/_at/_all functions to do what I'm trying to do. I've tried to learn across() several times and I either can't figure out how to make it work or it's very painful. I'd frankly be onboard with reverting to the legacy functions, but I'm sure that won't happen so some better examples in the doc would certainly help.
For across() on all columns I use something like that:
library(dplyr)
library(tibble)
library(stringr)
mydatas <- (mtcars
%>%tibble::rownames_to_column()
%>% rename (
mpg_aaa=mpg,
disp_bbb=disp,
model=rowname
)
%>% mutate(
brand=stringr::str_split_i(model ,"\\s", 1)
)
%>% select(
brand,
everything()
)
)
mydatas
mydatas_regroup <- (mydatas
%>% group_by(brand)
%>% summarise(
across(everything(),
~ (function (var,val) {
if (is.numeric(val) & (stringr::str_detect (var,"_aaa|_bbb")) ) {
round( sum(val, na.rm = TRUE),0)
} else if (is.numeric(val) ) {
round( sum(val, na.rm = TRUE),2)
} else {
paste0(
toString(
paste0(
"[",
sort(unique(val)),
"]",
collapse = ",")
)
)
}
})(cur_column(), .)
)
)
)
# A tibble: 22 × 13
brand model mpg_aaa cyl disp_bbb hp drat wt qsec vs am gear carb
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AMC [AMC Javelin] 15 8 304 150 3.15 3.44 17.3 0 0 3 2
2 Cadillac [Cadillac Fleetwood] 10 8 472 205 2.93 5.25 18.0 0 0 3 4
3 Camaro [Camaro Z28] 13 8 350 245 3.73 3.84 15.4 0 0 3 4
4 Chrysler [Chrysler Imperial] 15 8 440 230 3.23 5.34 17.4 0 0 3 4
5 Datsun [Datsun 710] 23 4 108 93 3.85 2.32 18.6 1 1 4 1
6 Dodge [Dodge Challenger] 16 8 318 150 2.76 3.52 16.9 0 0 3 2
7 Duster [Duster 360] 14 8 360 245 3.21 3.57 15.8 0 0 3 4
8 Ferrari [Ferrari Dino] 20 6 145 175 3.62 2.77 15.5 0 1 5 6
9 Fiat [Fiat 128],[Fiat X1-9] 60 8 158 132 8.16 4.14 38.4 2 2 8 2
10 Ford [Ford Pantera L] 16 8 351 264 4.22 3.17 14.5 0 1 5 4
# ℹ 12 more rows
# ℹ Use `print(n = ...)` to see more rows
PS: I'm a simple user
We do have a where(is.double) example near the top of the ?across examples, but we are missing one with everything().
I think we should add an example that uses everything() at the very top of the across() examples, because that is a very common case.