How to use lubridate and dplyr package In R Language
Lubridate package in R is used to provide easier working mechanisms with the date and time objects. It can be loaded and installed in the working space using the following command :
install.packages("lubridate")
The floor_date() method in R uses a date-time object, may be a single entity or vector of date-time objects, and then further rounds it off to the nearest integer value in the specified unit of time.
floor_date(x , unit = months)
The dplyr package is used to perform data manipulations and statistics. It can be loaded and installed in the working space using the following command :
install.packages("dplyr")
The dataframe is modified by using the piping operator over a sequence of operations and methods. The group_by() method is used to group the data based on the values contained in the specified columns.
group_by(col1,..)
Then a summary statistic is performed using the summarize() method which performs a summation over the values contained in the third column. The result is then manipulated into a dataframe using the as.data.frame() method.
Code:
R
library ( "dplyr" ) library ( "lubridate" ) set.seed (99923) # creating dataframe # specifying number of rows len <- 100 # creating sequences of dates var_seq <- seq ( as.Date ( "2021/05/01" ), by = "day" , length.out = len) # creating columns for dataframe data_frame <- data.frame (col1 = sample ( var_seq, 100, replace = TRUE ), col2 = round ( rnorm (10, 5, 2), 2)) print ( "Original dataframe" ) head (data_frame) # creating new month column for dataframe data_frame$month_year_col <- floor_date (data_frame$col1, "month" ) # aggregating the daily data data_frame_mod <- data_frame %>% group_by (month_year_col) %>% dplyr:: summarize (col2 = sum (col2)) %>% as.data.frame () print ( "Modified dataframe" ) head (data_frame_mod) |
Output:
Aggregate Daily Data to Month and Year Intervals in R DataFrame
In this article, we are going to see how to aggregate daily data over a period of months and year intervals in dataframe in R Programming Language.