Home > Back-end >  How can I create a new DF that summarise total annual value from yearly data (in R)?
How can I create a new DF that summarise total annual value from yearly data (in R)?

Time:09-26

Apologies if this Q has already been asked (I looked but the other Q&As didn't help). I'm trying to create a new df from an existing df that calculates total annual values from multiple columns and each row is one year. So, the df that I have looks something like this:

| year     | v1             | v2  | v3
| -------- | -------------- |-----|-----
| 2021     |  5             |4    |6
| 2021     | 10             |5    |3
| 2020     | 7              |8    |5
| 2020     | 3              |3    |4
| 2019     | 3              |1    |8

and what i'm trying to get is a new df that looks like:

| year     | v1             | v2  | v3
| -------- | -------------- |-----|-----
| 2021     | 15             |9    |9
| 2020     | 10             |11   |9
| 2019     | 3              |1    |8

I tried something like this without any luck:

newdf<- df %>% 
  group_by(year) %>%     # create the groups
  summarise(v1 = sum(v1))

Please let me know if I should include other information :) Thank you v.much in advance for your help!

CodePudding user response:

I use 'across-where' construction for this kind of tasks. This code select all columns which are numeric and sum all of them.

newdf<- df %>% 
  group_by(year) %>%     # create the groups
  summarise(across(where(is.numeric), sum, na.rm = T))

CodePudding user response:

Base R should do the trick:

newdf <- cbind(year = df$year, total = rowSums(x = df[, -1]))
  • Related