让 lubridate 减法仅返回数值
我有一个名为 Started
的变量,它是人类受试者参加研究的日期,另一个名为 dos1
的变量,它是受试者最后一次接受手术的日期。我想算出从他们上次手术到入组之日有多少个月了。我尝试过:
as.period(syrrupan$Started-syrrupan$dos1,units=c("month"))
我希望这会给我类似的结果:
14, 18, 1, 26
每个数字都是月数。
相反,我得到:
1 year, -4 months, -5 days and -1 hours 1 year, -5 months, -23 days and -1 hours 1 year, -7 months, 2 days and -1 hours 1 year, -8 months, -28 days and 1 hour 1 year, -7 months, -23 days and 1 hour.
我怎样才能得到月份的数值?
I have one variable called Started
which is the date on which human subjects enrolled in a study and another variable called dos1
which is the date upon which the subject last had surgery. I want to work out how many months since their last surgery to the day of enrollment. I tried:
as.period(syrrupan$Started-syrrupan$dos1,units=c("month"))
I expected this to give me something like:
14, 18, 1, 26
With each number being the number of months.
Instead I get:
1 year, -4 months, -5 days and -1 hours 1 year, -5 months, -23 days and -1 hours 1 year, -7 months, 2 days and -1 hours 1 year, -8 months, -28 days and 1 hour 1 year, -7 months, -23 days and 1 hour.
How can I get just the numeric value of months?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用
difftime
代替,即:请注意,这将为您提供一个
difftime
类的对象,如果您想要一个数字向量,请包装一个as.numeric围绕它。另请注意,您不能选择月份作为单位选项,但您确实应该坚持使用具有固定长度的时间单位。
You could try using
difftime
instead, ie:Note that this will give you an object of class
difftime
, if you want a numeric vector, wrap anas.numeric
around it. Note also that you can't choose months as an option for units, but you should really stick with a time unit that has a fixed length.如R - lubridate - 将周期转换为数字计数月份 这里预期的 lubridate 方法是
time_length
As noted in R - lubridate - Convert Period into numeric counting months the intended lubridate method here would be
time_length
这绝对是 lubridate 中的一个错误。我已经制作了错误报告,并将在 0.1 版本中修复它:
http://github. com/hadley/lubridate/issues#issue/75
感谢您提请我注意。
That's definitely a bug in lubridate. I've made an error report and will fix it for version 0.1:
http://github.com/hadley/lubridate/issues#issue/75
Thanks for bringing it to my attention.