Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

r - Have lubridate subtraction return only a numeric value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...