0

I'd like to aggregate time-series data to get weekly data, but doing so the class of the temporal variable becomes "character" instead of "Date", losing therefore any cool features of being a date.

This is quite annoying, especially when I need to plot data and play with breaks and labels.

Here is a short example of what I'm facing

# Storing some random daily data
require(plyr)
require(dplyr)
df = data.frame(date = seq.Date(from = as.Date('2013-01-01'),
                                to = as.Date('2014-12-31'),
                                by = 'day'),
                data = rnorm(365*2))

Aggregating the data into some weekly data

wdf = df %>%
    mutate(week = strftime(df$date, format = '%Y-%U')) %>%
    group_by(week) %>%
    summarise(wdata = max(data))

Unfortunately now the variable week is not of class "Date". Any idea about the possibility of keeping the class date for objects of the format %Y-%V?

Thanks in advance!

EB

Emiliano
  • 149
  • 1
  • 9
  • Date classed variable are integers with attributes. The results of any format operation is a character classed object. And ....How do you propose to keep groups of 7-day intervals as "Dates"? – IRTFM Mar 31 '15 at 23:01
  • Well, I was in fact asking. – Emiliano Apr 01 '15 at 00:15
  • 1
    Why import plyr? You only need dplyr here. – Ian Fiske Apr 01 '15 at 00:21
  • Asking for what exactly? Do you want weeks to be numbers or characters? Weeks would be numbered how exactly? Do you want them as 0-53 as per the POSIX standard? – IRTFM Apr 01 '15 at 00:25
  • @BondedDust I would like weeks to be date, not integers nor characters. – Emiliano Apr 01 '15 at 13:00

1 Answers1

1

Use the awesome lubridate package. It has a floor_date function that rounds a date downward according to any of several time units (including weeks as you want).

library(lubridate)

wdf = df %>%
  mutate(week = floor_date(date, unit = 'week')) %>%
  group_by(week) %>%
  summarise(wdata = max(data))
Ian Fiske
  • 10,482
  • 3
  • 21
  • 20
  • I actually already tried something similar. The problem is that the bars of the bar plot I want are really super narrow. I know I can change the width and that's actually exactly what I'm doing right now. I was just wondering whether there was a way to force a character string of type "%Y-%V" to keep the Date class. Probably a trick way to get through would be to replicate each weekly aggregated data each day of the week and then plot along the date variable. – Emiliano Apr 01 '15 at 12:59