有lubridate减法只返回一个数值 [英] Have lubridate subtraction return only a numeric value
问题描述
我有一个名为的变量开始
,这是人类受试者参加研究的日期,另一个变量叫做$ code> 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
,即:
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屋!