Robin Lindström
Robin Lindström

Reputation: 682

Adding name to summarised across columns using custom function

I want to use a custom function and return columns with an added "_cat_mean" to each column.

In the code below "$cat_mean" is added and I can't select it by that name.

summarise_categories <- function(x) { 
  tibble(
    cat_mean = round(mean(x) * 2) / 2
  )
  }

iris_summarised = iris %>% 
  group_by(Species) %>% 
  summarise(across(ends_with("Length"), ~summarise_categories(.)))

Select columns by the name which is displayed doesn't work

iris_summarised %>% 
  select(Species, Sepal.Length$cat_mean)

But this works

iris_summarised %>% 
  select(Species, Sepal.Length)

I want the column to be named "Sepal.Length_cat_mean"

Upvotes: 0

Views: 588

Answers (2)

akrun
akrun

Reputation: 886948

Using base R with colMeans and by

by(iris[-5], iris$Species, function(x) round(colMeans(x) * 2) /2)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388817

You can use .names argument in across to give new column names.

library(dplyr)

summarise_categories <- function(x) { 
    round(mean(x) * 2) / 2
}

iris %>% 
  group_by(Species) %>% 
  summarise(across(ends_with("Length"), summarise_categories, 
           .names = '{col}_cat_mean')) -> iris_summarised

iris_summarised

#  Species    Sepal.Length_cat_mean Petal.Length_cat_mean
#  <fct>                      <dbl>                 <dbl>
#1 setosa                       5                     1.5
#2 versicolor                   6                     4.5
#3 virginica                    6.5                   5.5

Upvotes: 4

Related Questions