iris %>% group_by(Species) %>% summarise(across( .cols = is.numeric, .fns = list(Sum = sum), na.rm = TRUE))
I'm grouping rows by species and them adding their values per column which are numeric.
Output looks like:
summarise() ungrouping output (override with .groups argument)
Species Sepal.Length_Sum Sepal.Width_Sum Petal.Length_Sum Petal.Width_Sum
<fct> <dbl> <dbl> <dbl> <dbl>
setosa 250. 171. 73.1 12.3
versicolor 297. 138. 213 66.3
virginica 329. 149. 278. 101.
I'd like to remove _Sum that is added after across function is applied.
R 4.0.0
dplyr 1.0.0
We don't need a named list
in that case
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across( .cols = is.numeric, .fns = sum, na.rm = TRUE))
Or if we still wanted to use a named list
, then specify the .names
which would give more control in adding prefix or suffix as well as removing anything extra by specifying only {col}
iris %>%
group_by(Species) %>%
summarise(across( .cols = is.numeric, .fns = list(Sum = sum),
.names = "{col}",na.rm = TRUE))
# A tibble: 3 x 5
# Species Sepal.Length Sepal.Width Petal.Length Petal.Width
# <fct> <dbl> <dbl> <dbl> <dbl>
#1 setosa 250. 171. 73.1 12.3
#2 versicolor 297. 138. 213 66.3
#3 virginica 329. 149. 278. 101.