R:如何获取月份的周数 [英] R: How to get the Week number of the month
问题描述
我是新的。
我想要这个日期所属的月份的周号。
I am new in R.
I want the week number of the month, which the date belongs to.
通过使用以下代码:
>CurrentDate<-Sys.Date()
>Week Number <- format(CurrentDate, format="%U")
>Week Number
"31"
%U将返回一年的周数。
但是我想要一个月的周数。
如果日期是2014-08-01,那么我想获得1.(日期属于本月的第一周)。
%U will return the Week number of the year .
But i want the week number of the month.
If the date is 2014-08-01 then i want to get 1.( The Date belongs to the 1st week of the month).
例如:
2014-09-04 - > 1(The日期属于本月的第一周)
2014-09-10 - > 2(日期属于本月的第二周)。
等等。
Eg:
2014-09-04 -> 1 (The Date belongs to the 1st week of the month).
2014-09-10 -> 2 (The Date belongs to the 2nd week of the month).
and so on...
我如何得到这个?
参考:
http://astrostatistics.psu.edu/su07/R/html/base/html/strptime.html
推荐答案
您可以使用 day
从 lubridate 包。我不知道包中是否有一个星期的类型功能,但我们可以做数学。
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
这篇关于R:如何获取月份的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!