让 lubridate 减法仅返回数值

发布于 2024-09-24 02:27:47 字数 567 浏览 3 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-10-01 02:27:47

您可以尝试使用 difftime 代替,即:

difftime(syrrupan$Started,syrrupan$dos1,units="days")

请注意,这将为您提供一个 difftime 类的对象,如果您想要一个数字向量,请包装​​一个 as.numeric围绕它。另请注意,您不能选择月份作为单位选项,但您确实应该坚持使用具有固定长度的时间单位。

You could try using difftime instead, ie:

difftime(syrrupan$Started,syrrupan$dos1,units="days")

Note that this will give you an object of class difftime, if you want a numeric vector, wrap an as.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.

雪化雨蝶 2024-10-01 02:27:47

As noted in R - lubridate - Convert Period into numeric counting months the intended lubridate method here would be time_length

time_length(syrrupan$Started-syrrupan$dos1, unit="days")
青春如此纠结 2024-10-01 02:27:47

这绝对是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文