Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bucketing data into weekly, bi-weekly, monthly and quarterly data in R

Tags:

date

r

I have a data frame with two columns. Date, Gender

I want to change the Date column to the start of the week for that observation. For example if Jun-28-2011 is a Tuesday, I'd like to change it to Jun-27-2011. Basically I want to re-label Date fields such that two data points that are in the same week have the same Date.

I also want to be able to do it by-weekly, or monthly and specially quarterly.

Update: Let's use this as a dataset.

datset <- data.frame(date = as.Date("2011-06-28")+c(1:100))
like image 238
Mark Avatar asked Jun 28 '11 15:06

Mark


2 Answers

One slick way to do this that I just learned recently is to use the lubridate package:

library(lubridate)
datset <- data.frame(date = as.Date("2011-06-28")+c(1:100))
#Add 1, since floor_date appears to round down to Sundays
floor_date(datset$date,"week") + 1

I'm not sure about how to do bi-weekly binning, but monthly and quarterly are easily handled with the respective base functions:

quarters(datset$date)
months(datset$date)

EDIT: Interestingly, floor_date from lubridate does not appear to be able to round down to the nearest quarter, but the function of the same name in ggplot2 does.

like image 195
joran Avatar answered Nov 19 '22 21:11

joran


Look at ?strftime. In particular, the following formats:

%b: Abbreviated month name in the current locale. (Also matches full name on input.)

%B: Full month name in the current locale. (Also matches abbreviated name on input.)

%m: Month as decimal number (01–12).

%W: Week of the year as decimal number (00–53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1). The UK convention.

eg:

> strftime("2011-07-28","Month: %B, Week: %W")
[1] "Month: July, Week: 30"

> paste("Quarter:",ceiling(as.integer(strftime("2011-07-28","%m"))/3))
[1] "Quarter: 3"
like image 24
James Avatar answered Nov 19 '22 21:11

James