-1

I am trying to get the week number from my EventDate column which has YYYY-MM-DD data, at the moment I am writing SQL in R but if anyone has any idea how to write R code only would be great too.

I have tried Datepart but it didn't work.

e.g :

EventDate     WeekNumber 
2020-02-03      ?
zx8754
  • 46,390
  • 10
  • 104
  • 180
Pegah
  • 53
  • 1
  • 3

2 Answers2

2

You can use the week function from lubridate:

library(lubridate)
some_dates <- ymd(c("2020-01-01", "2020-01-07", "2020-01-08", "2020-12-29", "2020-12-31"))
week(some_dates)
#> [1]  1  1  2 52 53

Created on 2020-07-15 by the reprex package (v0.3.0)

Calum You
  • 13,710
  • 3
  • 20
  • 39
1
> strftime("2020-02-03", format = "%V")
[1] "06"

> strftime(c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01"), format = "%V") 
[1] "11" "12" "12" "01"
Panwen Wang
  • 3,068
  • 1
  • 14
  • 33