Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I group days into weeks?

Tags:

date

r

In R, how can I associate between a vector of dates (days) and weeks?

Edit: "weeks" should be the week index within the date range and not within the year. My apologies for the ambiguity.

like image 853
JohnRos Avatar asked Nov 06 '11 22:11

JohnRos


People also ask

How do I group daily data into weeks in Excel?

Click a cell in the date column of the pivot table that Excel created in the spreadsheet. Right-click and select "Group," then "Days." Enter "7" in the "Number of days" box to group by week. Click "OK" and verify that you have correctly converted daily data to weekly data.

How do I group by Day in Excel?

Select any cell in the Date column in the Pivot Table. Go to Pivot Table Tools –> Analyze –> Group –> Group Selection. In the Grouping dialogue box, select Days and deselect any other selected option(s).

Why are my dates not grouping in Excel?

Reason 1: Grouping dates in filters is disabled In Excel, go to File. Click on Options (usually in the left bottom corner of the screen). Go to the Advanced tab in the left pane of the Options window). Scroll down to the workbook settings and set the check at “Group dates in the AutoFilter menu”.


2 Answers

For each date, get the week of the year it belongs to by formatting it via format() using the %U of %W format placeholders. %U treats Sunday as the first day of the week, whereas %W considers Monday to be the first day of the week. Here is an example:

now <- as.Date(Sys.time())
dates <- seq(now, now + 25, by = "1 day") 

dat <- data.frame(Dates = dates, Week = format(dates, format = "%W"))
head(dat, 10)

Which gives:

> head(dat, 10)
        Dates Week
1  2011-11-06   44
2  2011-11-07   45
3  2011-11-08   45
4  2011-11-09   45
5  2011-11-10   45
6  2011-11-11   45
7  2011-11-12   45
8  2011-11-13   45
9  2011-11-14   46
10 2011-11-15   46

Although it is unclear from your question exactly what you want to do, a way to match this vector of dates (well, my data frame dat above) is via merge(). Say we have a vector of weeks we want to match against and some associated data in data frame weekdat:

weekdat <- data.frame(Week = 44:50, Price = c(10, 20, 25, 30, 20, 15, 10))

Then we can link dat with weekdat using merge() as so:

> merge(dat, weekdat)
   Week      Dates Price
1    44 2011-11-06    10
2    45 2011-11-07    20
3    45 2011-11-08    20
4    45 2011-11-09    20
5    45 2011-11-10    20
6    45 2011-11-11    20
7    45 2011-11-12    20
8    45 2011-11-13    20
9    46 2011-11-14    25
10   46 2011-11-15    25
11   46 2011-11-16    25
12   46 2011-11-17    25
13   46 2011-11-18    25
14   46 2011-11-19    25
15   46 2011-11-20    25
16   47 2011-11-21    30
17   47 2011-11-22    30
18   47 2011-11-23    30
19   47 2011-11-24    30
20   47 2011-11-25    30
21   47 2011-11-26    30
22   47 2011-11-27    30
23   48 2011-11-28    20
24   48 2011-11-29    20
25   48 2011-11-30    20
26   48 2011-12-01    20

There are other ways of matching or linking dates to weeks, but the above should give you something to work from.

like image 102
Gavin Simpson Avatar answered Oct 14 '22 12:10

Gavin Simpson


Do you just want the number of completed 7 day intervals since the first record?

dvec <- as.Date("2001-04-01")+0:90
dweek <- as.numeric(dvec-dvec[1]) %/% 7
dweek[1:21]
# [1] 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 2 2
like image 21
IRTFM Avatar answered Oct 14 '22 13:10

IRTFM