如何根据R中的年龄和当前日期计算生日
How to calculate birthdate from age and current date in R
我只有参与者的年龄和当前日期(年龄以年份显示,如 56.07231)。现在我需要知道他们的出生日期。
我如何在 R 中做到这一点?我在网上只能找到如何根据两个日期计算年龄。谢谢!
一个可能的解决方案是使用 lubridate 包中的持续时间对象。
您的问题并不具体,但该包可以处理不同的方法来计算时间,还可以使用 %m-% 运算符减去日期。
下面的例子
library(lubridate)
##If the person is 57.0631 days old, you can get their birth date by doing
ymd("2021-07-06") %m-% seconds(ddays(57.0631)@.Data) ##2021-05-10
##If the person is 57.0631 years old, you can do:
ymd("2021-07-06") %m-% seconds(dyears(57.0631)@.Data) ##1964-06-26
我只有参与者的年龄和当前日期(年龄以年份显示,如 56.07231)。现在我需要知道他们的出生日期。
我如何在 R 中做到这一点?我在网上只能找到如何根据两个日期计算年龄。谢谢!
一个可能的解决方案是使用 lubridate 包中的持续时间对象。 您的问题并不具体,但该包可以处理不同的方法来计算时间,还可以使用 %m-% 运算符减去日期。
下面的例子
library(lubridate)
##If the person is 57.0631 days old, you can get their birth date by doing
ymd("2021-07-06") %m-% seconds(ddays(57.0631)@.Data) ##2021-05-10
##If the person is 57.0631 years old, you can do:
ymd("2021-07-06") %m-% seconds(dyears(57.0631)@.Data) ##1964-06-26