Pedro BP
Pedro BP

Reputation: 73

Calculate a mean by groups in R

I'd like to compute the mean by Mainland in each year.

The result would be something like:

Mainland 1990 1991 1992 1993 ...
Europe     1    55  66   678
Asia       43   6    7
Oceania          .
Americas         .
Africa           .

Firstly, I selected the columns I need and then tried to do the calculation using the dplyr package in R but it doesnt work.

gr1 <- homicide_ratios %>% 
  select('Mainland', matches("[0-9]{4}")) 

gr1 %>% 
  group_by(Mainland) %>% 
  summarise(media = mean(gr1[, 2:ncol(gr1)], na.rm = T))

I show you the dataset:

enter image description here

Thanks in advance.

Upvotes: 0

Views: 209

Answers (1)

Nareman Darwish
Nareman Darwish

Reputation: 1261

The idea is to change the format of the data from wide format into long format and then group the data and summarize it as follows;

library(dplyr)
library(tidyr)

homicide_ratios <-
  data.frame(
    Mainland = c("Europe", "Asia", "Oceania", "Americas", "Africa"),
    "1990" = c(1, 2, 3, 4, 5),
    "1991" = c(1, 2, 3, 4, 5),
    "1992" = c(1, 2, 3, 4, 5),
    "1993" = c(1, 2, 3, 4, 5)
  )

homicide_ratios %>%
  gather(key = "year", value = "rate", -Mainland) %>%
  group_by(Mainland, year) %>%
  summarize(average = mean(rate))

# # A tibble: 20 x 3
# # Groups:   Mainland [5]
# Mainland year  average
# <fct>    <chr>   <dbl>
#   Africa   X1990       5
#   Africa   X1991       5
#   Africa   X1992       5
#   Africa   X1993       5
#   Americas X1990       4
#   Americas X1991       4
#   Americas X1992       4

Upvotes: 3

Related Questions