R Group & Summarize Daily Data to Month & Year Using dplyr (Example Code)

In this tutorial, I’ll show how to summarize and group daily data into monthly intervals in R programming.

Example Data

df <- data.frame(dates = seq(as.Date("2022/10/01"),  # Example data in R
                             by = "day",
                             length.out = 500),
                 x = 1:500)
head(df)                                             # Show head of example data in RStudio console
#        dates x
# 1 2022-10-01 1
# 2 2022-10-02 2
# 3 2022-10-03 3
# 4 2022-10-04 4
# 5 2022-10-05 5
# 6 2022-10-06 6

Example: Group & Summarize Daily Data to Month & Year

install.packages("lubridate")                        # Install & load lubridate package
library("lubridate")
df$ym <- floor_date(df$dates, "month")               # Apply floor_date function
install.packages("dplyr")                            # Install dplyr package
library("dplyr")                                     # Load dplyr package
df_sum <- df %>%                                     # Apply group_by & summarize functions
  group_by(ym) %>% 
  dplyr::summarize(x = mean(x))
df_sum                                               # Show aggregated data in RStudio
# # A tibble: 17 x 2
#    ym             x
#    <date>     <dbl>
#  1 2022-10-01  16  
#  2 2022-11-01  46.5
#  3 2022-12-01  77  
#  4 2023-01-01 108  
#  5 2023-02-01 138. 
#  6 2023-03-01 167  
#  7 2023-04-01 198. 
#  8 2023-05-01 228  
#  9 2023-06-01 258. 
# 10 2023-07-01 289  
# 11 2023-08-01 320  
# 12 2023-09-01 350. 
# 13 2023-10-01 381  
# 14 2023-11-01 412. 
# 15 2023-12-01 442  
# 16 2024-01-01 473  
# 17 2024-02-01 494.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top