-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
  • 52,746
  • 12
  • 114
  • 209
Pegah
  • 53
  • 1
  • 1
  • 4

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
  • 14,687
  • 4
  • 23
  • 42
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,573
  • 1
  • 18
  • 39