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.
)
Asked
Active
Viewed 41 times
0

Ronak Shah
- 377,200
- 20
- 156
- 213

Paul Cuc
- 17
- 4
-
1The general syntax is `df2 <- df1 %>% group_by(Genul) %>% summarise(across(c(col1, col2), mean, na.rm = TRUE))`. Replace `col1` and `col2` with your column names. – Ronak Shah Mar 28 '21 at 07:03
-
df3 <- ddply(df1, .(Genul), summarize, Q1=mean(col1), Q2=mean(col2)). found a simpler method – Paul Cuc Mar 28 '21 at 07:13
1 Answers
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
- 72,363
- 6
- 19
- 66