r Grouping columns and calculating averages in all columns

Asked

Viewed 371 times

2

I have this data set:

mes <- c("jan","fev","jan","abr","fev","abr","jan","fev","abr")
a <- c(32.3,32.7,32.6,33.1,33.0,33.5,33.4,33.4,34.9)
b <- c(19.2,19.2,19.6,19.7,19.7,19.9,20.0,20.0,20.4)
c <- c(14.7,15.0,15.6,16.2,16.4,17.0,17.7,18.3,19.1)
d <- c(24.2,24.3,24.7,25.0,25.5,26.4,26.7,27.1,27.6) 
temp <- data.frame(mes,a,b,c,d)

I am grouping as follows, using the package dplyr

base <- temp %>% 
  group_by(mes) %>% 
  summarise(n = length(mes), med.a = mean(a), 
            med.b=mean(b),med.c=mean(c),med.d=mean(d))

I’d like to group by the column mes of data.frame counting the number of observations and also calculating the averages for all columns at once, without having to name each new column that will be calculated the average.

has how to do this procedure? I would like to write in a way that I could use other data.frame with different column numbers without always having to name them. For the process to be automatic and run in other databases I have.

1 answer

2


Create a new column with counts using mutate. Next, use summarise_each to tell which function should be applied to each variable, except the first.

library(dplyr)
temp %>%
  mutate(n = n()) %>%
  summarise_each(mean, -1)
##         a        b        c        d n
##1 33.21111 19.74444 16.66667 25.72222 9

This solution assumes that the first column will always be categorical. If the categorical column is called mes, it is possible to solve this problem by calling it by its name:

temp %>%
  mutate(n = n()) %>%
  summarise_each(mean, -mes)
##         a        b        c        d n
##1 33.21111 19.74444 16.66667 25.72222 9

Browser other questions tagged

You are not signed in. Login or sign up in order to post.