-2

I'm working on a dataframe called 'df'. Two of the columns in 'df' are 'country' and 'dissent'. I need to calculate the average dissent per country. What is the most effective way to iterate through the data frame and calculate the averages by country?

I tried for loops but it does not work and also I don't think it is the most effective way.

fg42
  • 71
  • 7
  • 1
    To add to the resource recommended by @markus, take a look at `?aggregate` or `?by`, or `?dplyr::group_by` and `?dplyr::summarise` – duckmayr Jul 10 '19 at 12:54

1 Answers1

2

tidyverse provides the easiest way IMO

df %>% group_by(country) %>% summarize(avg = mean(dissent, na.rm = TRUE))

If you really are intent upon "iterating" through, as the question suggests (though not recommended):

avg <- NULL
for (i in 1:length(unique(df$country))) {
  avg[i] <- mean(df[df$country == unique(df$country)[i], "dissent"], na.rm=TRUE)
}
Dij
  • 1,318
  • 1
  • 7
  • 13