From Chaos to Clarity: Mastering Weekly Data Wrangling in R with strftime()

code
rtip
timeseries
Author

Steven P. Sanderson II, MPH

Published

February 12, 2024

Introduction

Grouping data by week is a common task in data analysis. It allows you to summarize and analyze your data on a weekly basis. In R, there are a few different ways to group data by week, but one easy method is using the strftime() function.

The strftime() function converts a date-time object to a string in a specified format. By using the format %V, we can extract the week number from a date. Let’s walk through an example:

First, let’s create a data frame with some date values:

dates <- as.Date(c("2023-01-01", "2023-01-15", "2023-02-05", "2023-02-17", "2023-03-01"))
values <- c(1.5, 3.2, 2.7, 4.1, 2.3) 

df <- data.frame(dates, values)
df
       dates values
1 2023-01-01    1.5
2 2023-01-15    3.2
3 2023-02-05    2.7
4 2023-02-17    4.1
5 2023-03-01    2.3

Now we can use strftime() to extract the week number as follows:

df$week <- strftime(df$dates, format = "%V")
df
       dates values week
1 2023-01-01    1.5   52
2 2023-01-15    3.2   02
3 2023-02-05    2.7   05
4 2023-02-17    4.1   07
5 2023-03-01    2.3   09

This adds a new column week to the data frame containing the week number for each date.

We can now easily group the data by week and summarize the values column:

aggregate(values ~ week, df, mean)
  week values
1   02    3.2
2   05    2.7
3   07    4.1
4   09    2.3
5   52    1.5

And there we have it! The data neatly summarized by week. The %V format in strftime() makes it easy to group by week in R.

I encourage you to try this on your own data. Converting dates to week numbers enables all sorts of weekly time series analyses. Let me know if you have any other questions!