R: How to get the Week number of the month

前端 未结 9 835
孤街浪徒
孤街浪徒 2020-11-30 04:05

I am new in R.
I want the week number of the month, which the date belongs to.

By using the following code:

>CurrentDate<-Sys.Date()
>We         


        
9条回答
  •  Happy的楠姐
    2020-11-30 04:40

    You can use day from the lubridate package. I'm not sure if there's a week-of-month type function in the package, but we can do the math.

    library(lubridate)
    curr <- Sys.Date()
    # [1] "2014-08-08"
    day(curr)               ## 8th day of the current month
    # [1] 8
    day(curr) / 7           ## Technically, it's the 1.14th week
    # [1] 1.142857
    ceiling(day(curr) / 7)  ## but ceiling() will take it up to the 2nd week.
    # [1] 2
    

提交回复
热议问题