Sum quarters and conversion to year

I have a df with this structure (date have date format, value is numeric and name is character)

lows <- data.frame(
  stringsAsFactors = FALSE,
              name = c("1º2004","2º2004","3º2004",
                       "4º2004","1º2005","2º2005","3º2005","4º2005"),
             value = c(299L, 395L, 361L, 333L, 288L, 286L, 352L, 406L),
              date = c("2004-01-01","2004-04-01",
                       "2004-07-01","2004-10-01","2005-01-01","2005-04-01",
                       "2005-07-01","2005-10-01")
)

Now, I want to obtain the total value for each year, not for each quarter, i.e. 2004 =299+395+361+333, that is 1388 and so on... and made a new dataframe with this results, something like name=2004, value=1388, date=2004 and so on. Any idea? Maybe with lubridate?

The date column in the data frame you provided looks like a date but it is characters. In the code below, I make it a date and then extract the year.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
lows <- data.frame(
  stringsAsFactors = FALSE,
  name = c("1º2004","2º2004","3º2004",
           "4º2004","1º2005","2º2005","3º2005","4º2005"),
  value = c(299L, 395L, 361L, 333L, 288L, 286L, 352L, 406L),
  date = c("2004-01-01","2004-04-01",
           "2004-07-01","2004-10-01","2005-01-01","2005-04-01",
           "2005-07-01","2005-10-01")
)
#Make the date column a date and extract the year value
lows <- lows |> mutate(date = ymd(date),
                       Year = year(date))
lows
#>     name value       date Year
#> 1 1º2004   299 2004-01-01 2004
#> 2 2º2004   395 2004-04-01 2004
#> 3 3º2004   361 2004-07-01 2004
#> 4 4º2004   333 2004-10-01 2004
#> 5 1º2005   288 2005-01-01 2005
#> 6 2º2005   286 2005-04-01 2005
#> 7 3º2005   352 2005-07-01 2005
#> 8 4º2005   406 2005-10-01 2005

#Summarize by year
Sums <- lows |> group_by(Year) |> summarize(Total = sum(value))
Sums
#> # A tibble: 2 x 2
#>    Year Total
#>   <dbl> <int>
#> 1  2004  1388
#> 2  2005  1332

Created on 2022-03-11 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.