0

I'm trying to take the means of some data in terms of women and men and put it in a new dataframe. I can manage to do it for two columns using dplyr but, not for the whole dataframe. I used: df2 <- df1 %>% group_by(Genul) %>% summarise(average = mean(Apreciez că în condițiile actuale de pandemie, compania (Hotelul) în cadrul căreia sunt angajat a luat măsuri eficiente și suficiente de prevenție împotriva răspândirii virusului Sars-Cov-2 si contaminării cu acesta.)

enter image description here enter image description here

Ronak Shah
  • 355,584
  • 18
  • 123
  • 178
Paul Cuc
  • 17
  • 4

1 Answers1

1

You can create your own summary function my_mean using {{}} and across

Then apply your function my_mean to the columns you want to calculate the mean

See below example with mtcars dataset:

library(dplyr)
my_mean <- function(data, col_names, na.rm = TRUE) {
  data %>% 
    summarise(across({{col_names}},
              list(mean = mean),
              na.rm = na.rm,
              .names = "{col}_{fn}"
    ))
}

mtcars %>% 
  group_by(cyl) %>% 
  my_mean(c(mpg, disp, hp, drat))

# Output:
# A tibble: 3 x 5
    cyl mpg_mean disp_mean hp_mean drat_mean
  <dbl>    <dbl>     <dbl>   <dbl>     <dbl>
1     4     26.7      105.    82.6      4.07
2     6     19.7      183.   122.       3.59
3     8     15.1      353.   209.       3.23
TarJae
  • 43,365
  • 4
  • 14
  • 40