Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I group my date variable into month/year in R?

Tags:

r

I have a "date" vector, that contains dates in mm/dd/yyyy format:

 head(Entered_Date,5) [1] 1/5/1998 1/5/1998 1/5/1998 1/5/1998 1/5/1998 

I am trying to plot a frequency variable against the date, but I want to group the dates that it is by month or year. As it is now, there is a frequency per day, but I want to plot the frequency by month or year. So instead of having a frequency of 1 for 1/5/1998, 1 for 1/7/1998, and 3 for 1/8/1998, I would like to display it as 5 for 1/1998. It is a relatively large data set, with dates from 1998 to present, and I would like to find some automated way to accomplish this.

> dput(head(Entered_Date)) structure(c(260L, 260L, 260L, 260L, 260L, 260L), .Label = c("1/1/1998",  "1/1/1999", "1/1/2001", "1/1/2002", "1/10/2000", "1/10/2001",  "1/10/2002", "1/10/2003", "1/10/2005", "1/10/2006", "1/10/2007",  "1/10/2008", "1/10/2011", "1/10/2012", "1/10/2013", "1/11/1999",  "1/11/2000", "1/11/2001", "1/11/2002", "1/11/2005", "1/11/2006",  "1/11/2008", "1/11/2010", "1/11/2011", "1/11/2012", "1/11/2013",  "1/12/1998", "1/12/1999", "1/12/2001", "1/12/2004", "1/12/2005", ... 
like image 201
Learning_R Avatar asked Oct 19 '15 18:10

Learning_R


People also ask

How do I group dates together in R?

If you've got a series of dates and associated values, there's an extremely easy way to group them by date range such as week, month, quarter or year: R's cut() function. The as. Date() function is important here; otherwise R will view each item as a string object and not a date object.


1 Answers

The floor_date() function from the lubridate package does this nicely.

data %>%      group_by(month = lubridate::floor_date(date, "month")) %>%     summarize(summary_variable = sum(value)) 

Thanks to Roman Cheplyaka https://ro-che.info/articles/2017-02-22-group_by_month_r

See more on how to use the function: https://lubridate.tidyverse.org/reference/round_date.html

like image 65
Hugh Sawbridge Avatar answered Sep 25 '22 20:09

Hugh Sawbridge