문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top