让 lubridate 减法只返回一个数值 [英] Have lubridate subtraction return only a numeric value
问题描述
我有一个名为 Started
的变量,它是人类受试者参加研究的日期,另一个名为 dos1
的变量是受试者最后一次参加研究的日期手术.我想计算从他们最后一次手术到入学之日的几个月.我试过了:
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
每个数字都是月数.
相反,我得到:
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?
推荐答案
你可以尝试使用 difftime
代替,即:
You could try using difftime
instead, ie:
difftime(syrrupan$Started,syrrupan$dos1,units="days")
请注意,这将为您提供一个 difftime
类的对象,如果您想要一个数字向量,请在其周围包裹一个 as.numeric
.另请注意,您不能选择月份作为单位选项,但您应该坚持使用具有固定长度的时间单位.
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.
这篇关于让 lubridate 减法只返回一个数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!