round_weeks: Round dates to weeks

Description Usage Arguments Details Note Author(s) Examples

View source: R/round_weeks.R

Description

Converts a vector of dates to weeks using data.table's IDate.

Usage

1

Arguments

x

Vector of dates

Details

This function uses Sunday as the first day of the week, and will round the vector of dates to the first day of the week. For other units of time (e.g. month, year) data.table:::round.IDate is suggested. Currently IDate's round function for 'weeks' will split any week overlapping two years into two week parts, which usually starts the rounded weeks in the second year on a new day. This function is a workaround for that behavior.

Note

More details about week rounding issue (mentioned in details) here: https://github.com/Rdatatable/data.table/issues/747

Depends on data.table

Author(s)

Gene Leynes

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
		require(data.table)
		require(geneorama)

 	## Create a sequence of dates (or IDates)
		dd <- seq(as.IDate("2013-12-22"), as.IDate("2014-01-18"), 1)
		## Weeks rounded using default data.table method:
		round(dd, 'weeks')
		## Weeks rounded using "round_weeks":
		round_weeks(dd)
		## Rounding methods shown side-by-side
		data.table(date_original = dd,
				   weeks_dt = round(dd, 'weeks'),
				   weeks_new = round_weeks(dd),
				   wday = wday(dd),
				   weekday = weekdays(dd))

geneorama/geneorama documentation built on Oct. 17, 2020, 12:35 a.m.