I have a similar looking df for which I want to calculate yearly sums and mean. Is there a quicker and more clever way to get exact the same output? Especially calculating the mean feels clumsy. I tried working with mean()
but didn“t succeed.
a <- 1:10
b <- 5:14
c <- 36:45
d <- 22:31
year <- rep(2010:2014, 2)
df <- data.frame(a,b,c,d, year)
df[2,c(1,3,4)] <- NA
df[5,c(1,4)] <- NA
df[8,c(1,2)] <- NA
df_sums <- df %>% select(a, b, c, d, year) %>%
group_by(year) %>%
summarise(a_sum=sum(a, na.rm = T),
b_sum=sum(b, na.rm = T),
c_sum=sum(c, na.rm = T),
d_sum=sum(d, na.rm = T),
mean=(a_sum +
b_sum +
c_sum +
d_sum)/4)