问题描述
我正在尝试获取lubridate的日期序列
Hi I'm trying to get a sequence of dates with lubridate
这不起作用
seq(ymd('2012-04-07'),ymd('2013-03-22'),by=week(1))
基本命令
seq(as.Date('2012-04-7'),as.Date('2013-03-22'),'weeks')
可以,但是我想知道是否有一种使用lubridate的优雅方法.
does, but I'd like to know if there is an elegant way to do this with lubridate.
编辑
请忽略:解决了我自己,只留给后代.很高兴在必要时将其删除.
Please ignore : solved myself so leaving up for posterity only. Happy to have this deleted if necessary.
seq(ymd('2012-04-07'),ymd('2013-03-22'),by='weeks')
有花招
推荐答案
ymd
是用于解析日期字符串并返回POSIXct
对象的包装器.
ymd
is a wrapper to parse date strings and returns a POSIXct
object.
您只需要使用?seq.POSIXt
(不是lubridate
)中描述的标准术语来定义星期
You simply need to use standard terminology described in ?seq.POSIXt
(not lubridate
) to define weeks
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = '1 week')
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = 'weeks')
将起作用
将
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = '2 week')
您可以将lubridate
Period
类对象强制为difftime
,但这似乎是不必要的
You could coerce the lubridate
Period
class object to a difftime
, but that seems rather unnecessary
seq(ymd('2012-04-07'),ymd('2013-03-22'), by = as.difftime(weeks(1)))
这篇关于R日期与lubridate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!