Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract week number from POSIXct object

Tags:

date

r

lubridate

Is there a function in lubridate to extract the week number? I've tried to search for that but couldn't find anything which serves the purpose.

The week() function does something different.

Description

Date-time must be a POSIXct, POSIXlt, Date, chron, yearmon, yearqtr, zoo, zooreg, timeDate, xts, > its, ti, jul, timeSeries, and fts objects. Weeks is the number of complete seven day periods that have occured between the date and January 1st, plus one. isoweek returns the week as it would appear in the ISO 8601 system, which uses a reoccuring leap week.

like image 626
Gianluca Avatar asked Sep 16 '14 10:09

Gianluca


People also ask

How do I get week number in R?

How to get the week number from a date. To get the ISO week number (1-53), use lubridate::isoweek( date ) or format( date , "%-V") . To get the corresponding four-digit year (e.g. 2022), use lubridate::isoyear( date ) or format( date , "%G") .

What is the difference between POSIXct and Posixt?

The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.

What does wday do in r?

wday() returns the day of the week as a decimal number or an ordered factor if label is TRUE .


1 Answers

Use strftime:

dateRange <- c("2008-10-01","2008-12-01") 
x <- as.POSIXlt(dateRange) 
strftime(x,format="%W") 
[1] "39" "48" 
like image 69
Prasanna Nandakumar Avatar answered Oct 28 '22 19:10

Prasanna Nandakumar