是否有一个 R 包可以处理 POSIX 对象并返回一周的第 n 天?
我编写了一个函数,当提供一个日期范围、一周中特定日期的名称以及该日期在给定月份(例如,每个月的第二个星期五)中的出现时,将返回对应的日期.然而,它不是很快,我也不是 100% 相信它的健壮性.R 中是否有一个包或一组函数可以对 POSIX 对象执行这些类型的操作?提前致谢!
I have written a function which, when provided a range of dates, the name of a particular day of the week and the occurrence of that day in a given month (for instance, the second Friday of each month) will return the corresponding date. However, it isn't very fast and I'm not 100% convinced of its robustness. Is there a package or set of functions in R which can do these kinds of operations on POSIX objects? Thanks in advance!
使用 nextfri
函数,其一行源码显示在 动物园快速参考小插图在 zoo package 下面给出了 d
的第二个星期五,其中 d
是 日期"
第一个月:
Using the function nextfri
whose one line source is shown in the zoo Quick Reference vignette in the zoo package the following gives the second Friday of d
where d
is the "Date"
of the first of the month:
> library(zoo)
> d <- as.Date(c("2011-09-01", "2011-10-01"))
> nextfri(d) + 7
[1] "2011-09-09" "2011-10-14"
(nextfri
不是动物园包的一部分——你需要自己输入——但它只有一行)
(nextfri
is not part of the zoo package -- you need to enter it yourself -- but its only one line)
下面给出了星期几,其中 0 是星期日,1 是星期一,等等.
The following gives the day of the week where 0 is Sunday, 1 is Monday, etc.
> as.POSIXlt(d)$wday
[1] 4 6
如果你真的只处理日期而不是日期时间,那么你应该使用 "Date"
类而不是 "POSIXt"
类以避免时区错误.请参阅 R 新闻 4/1 中的文章.
If you really are dealing exclusively with dates rather than date-times then you ought to be using "Date"
class rather than "POSIXt"
classes in order to avoid time zone errors. See the article in R News 4/1.