0

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.

Pedro BP
  • 73
  • 5
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, since we can't run code on a picture of a table. There are a lot of posts already on calculating summary statistics by group—what about those hasn't worked? – camille Jan 02 '20 at 17:43
  • I thiink you neeed `summarise_all` `gr1 %>% group_by(Mainland) %>% summarise_all(vars(-group_cols(), mean)` – akrun Jan 02 '20 at 17:47
  • Does this answer your question? [Summarizing multiple columns with dplyr?](https://stackoverflow.com/questions/21644848/summarizing-multiple-columns-with-dplyr) – camille Jan 02 '20 at 17:48

1 Answers1

2

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
Nareman Darwish
  • 1,251
  • 7
  • 14