How to summarize and group daily data into monthly intervals in the R programming language. More details: statisticsglob...
R code of this video:
set.seed(8965379) # Create random example data
data <- data.frame(date = sample(seq(as.Date("2020/01/01"),
by = "day",
length.out = 1000),
100, replace = TRUE),
value = round(rnorm(100, 5, 2), 2))
data_new1 <- data # Duplicate data
data_new1$year <- strftime(data_new1$date, "%Y") # Create year column
data_new1$month <- strftime(data_new1$date, "%m") # Create month column
data_aggr1 <- aggregate(value ~ month + year, # Aggregate data
data_new1,
FUN = sum)
install.packages("lubridate") # Install & load lubridate
library("lubridate")
data_new2 <- data # Duplicate data
data_new2$year_month <- floor_date(data_new2$date, # Create year-month column
"month")
install.packages("dplyr") # Install dplyr package
library("dplyr") # Load dplyr
data_aggr2 <- data_new2 %>% # Aggregate data
group_by(year_month) %>%
dplyr::summarize(value = sum(value)) %>%
as.data.frame()
Follow me on Social Media:
Facebook - Statistics Globe Page: / statisticsglobecom
Facebook - Group for Discussions & Questions: / statisticsglobe
LinkedIn - Statistics Globe Page: / statisticsglobe
LinkedIn - Group for Discussions & Questions: / 12555223
Twitter: / joachimschork
Music by bensound.com
Негізгі бет Aggregate Daily Data to Month & Year Intervals in R | Base R & dplyr Example | lubridate Package
Пікірлер: 110