计算R中的年龄(小数位) [英] Calculate age (with decimal places) in R
本文介绍了计算R中的年龄(小数位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两组日期
Testday< -as.Date(c(2015-06-25 2015-06-25,2015-06-25))
DOB< -as.Date(c(2012-03-15,2004-04-21,2014-07 -25))
我想计算年龄,正确到2位小数。
在 Excel
中,我会这样做:
(Testday - DOB)/365.25
并获得这些年龄: 3.28,11.18,0.92
/ p>
是否还可以在 R
上执行此操作?
解决方案
这是否可以?
round(as.numeric((Testday - DOB)/ 365.25),2)
#[1] 3.28 11.18 0.92
I have two sets of dates
Testday<-as.Date(c("2015-06-25", "2015-06-25","2015-06-25"))
DOB<-as.Date(c("2012-03-15", "2004-04-21","2014-07-25"))
I want to calculate the age, correct to 2 decimal places.
On Excel
, I would simply do this:
(Testday - DOB)/365.25
and obtain these ages: 3.28, 11.18, 0.92
Is there anyway I could perform this on R
too?
解决方案
Is this OK?
round(as.numeric((Testday - DOB) / 365.25), 2)
#[1] 3.28 11.18 0.92
这篇关于计算R中的年龄(小数位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文