I have a dataframe with more than 190,000 rows, something a bit like this:
library(tibble)
mydf <- tribble(~col1, ~col2, ~col3, ~col4, ~col5,
"A", 16, 45, 53, 35,
"A", 17, 12, 54, 12,
"A", 19, 12, 54, 35,
"B", 10, 87, 55, 22,
"B", 10, 87, 55, 22,
"B", 12, 23, 12, 67)
There are repeated iterations of col1
; some have the same values across the columns, others have different values across columns, as shown in the sample dataframe.
For each repeated level in col1
, I want to aggregate these values into one row showing the mean of all the rows. I got so far using this answer, however, this leaves me with all the distinct rows:
mydf %>% group_by(col1) %>%
mutate_each(funs(mean), -(1)) %>%
distinct()
# A tibble: 5 x 5
# Groups: col1 [2]
col1 col2 col3 col4 col5
<chr> <dbl> <dbl> <dbl> <dbl>
1 A 16 23 53.7 27.3
2 A 17 23 53.7 27.3
3 A 19 23 53.7 27.3
4 B 10 65.7 40.7 37
5 B 12 65.7 40.7 37
What I actually want here is one row for A
, B
, etc, showing the mean values.