根据任何以前的时间计算年龄

calculation of age based on any preveous time

有没有办法找到这样的问题的解决方案;

如果我 10/jan/2010 的年龄是 2 岁,那么 10/April/2012 的年龄是多少?

亲切的问候

只需使用difftime()计算差值,然后加上年龄即可。像这样:

earlier_age <- 2
diff <- as.numeric(difftime(latest_date, earlier_date), units="years") 
#make sure that dates are actually date objects (using as.Date())
new_age <- diff + earlier_age


#So for your example:
latest_date <- as.Date("10/April/2012", format = "%d/%B/%Y")
earlier_date <- as.Date("10/jan/2010", format = "%d/%b/%Y")
earlier_age <- 2

diff <- as.numeric(difftime(latest_date, earlier_date), units="days")/365.25
new_age <- diff + earlier_age

您可以根据需要将单位更改为周、小时等

您可以设置日期,例如:

x <- as.Date("2010-01-10")
y <- as.Date("2012-04-10")

>y-x
Time difference of 821 days

从这里开始,再加上 2 年。如果这有帮助,请告诉我!