有一个lubridate减法只返回一个数值

Far*_*rel 16 r date package lubridate

我有一个变量叫做Started人类受试者参加研究dos1的日期和另一个被称为受试者最后接受手术的日期.我想知道从他们上次手术到入学当天有多少个月.我试过了:

as.period(syrrupan$Started-syrrupan$dos1,units=c("month"))
Run Code Online (Sandbox Code Playgroud)

我希望这给我一些类似的东西:

14, 18, 1, 26 
Run Code Online (Sandbox Code Playgroud)

每个数字是月数.

相反,我得到:

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.   
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到几个月的数值?

Jam*_*mes 30

您可以尝试使用difftime,即:

difftime(syrrupan$Started,syrrupan$dos1,units="days")
Run Code Online (Sandbox Code Playgroud)

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

  • 在这种情况下,您可以将上述结果除以30.44. (2认同)

Hol*_*ndl 11

R - lubridate - 将期间转换为数字计数月份中所述,此处预期的 lubridate 方法将是time_length

time_length(syrrupan$Started-syrrupan$dos1, unit="days")
Run Code Online (Sandbox Code Playgroud)


小智 5

这绝对是lubridate中的错误。我做了一个错误报告,并将针对版本0.1进行修复:

http://github.com/hadley/lubridate/issues#issue/75

感谢您引起我的注意。