我在R中有两个数据框,其中一个数据框包含人的出生年份:
YEAR
/1931
/1924
然后另一列显示较新的时间。
RECENT
09/08/2005
11/08/2005
我想做的是减去年份,以便可以计算他们的年龄,但我不确定如何处理。 请帮忙吗?
我在R中有两个数据框,其中一个数据框包含人的出生年份:
YEAR
/1931
/1924
然后另一列显示较新的时间。
RECENT
09/08/2005
11/08/2005
我想做的是减去年份,以便可以计算他们的年龄,但我不确定如何处理。 请帮忙吗?
age = function(from, to) {
from_lt = as.POSIXlt(from)
to_lt = as.POSIXlt(to)
age = to_lt$year - from_lt$year
ifelse(to_lt$mon < from_lt$mon |
(to_lt$mon == from_lt$mon & to_lt$mday < from_lt$mday),
age - 1, age)
}
ifelse
(https://dev59.com/fGQo5IYBdhLWcg3wKcrH),代码如下:`out <- integer(length(year)); out[idx <- to_lt$mon < from_lt$mon] <- age - 1; out[!idx] <- age]` - MichaelChirico]
,即使这样也不能正常工作。想象一下你是1980年12月出生的,现在是2018年12月...你自己算吧。 - MS Berends我使用自定义函数,如下所示的代码,方便在 mutate 中使用,而且非常灵活(需要 lubridate
包)。
例子
get_age("2000-01-01")
# [1] 17
get_age(lubridate::as_date("2000-01-01"))
# [1] 17
get_age("2000-01-01","2015-06-15")
# [1] 15
get_age("2000-01-01",dec = TRUE)
# [1] 17.92175
get_age(c("2000-01-01","2003-04-12"))
# [1] 17 14
get_age(c("2000-01-01","2003-04-12"),dec = TRUE)
# [1] 17.92176 14.64231
功能
#' Get age
#'
#' Returns age, decimal or not, from single value or vector of strings
#' or dates, compared to a reference date defaulting to now. Note that
#' default is NOT the rounded value of decimal age.
#' @param from_date vector or single value of dates or characters
#' @param to_date date when age is to be computed
#' @param dec return decimal age or not
#' @examples
#' get_age("2000-01-01")
#' get_age(lubridate::as_date("2000-01-01"))
#' get_age("2000-01-01","2015-06-15")
#' get_age("2000-01-01",dec = TRUE)
#' get_age(c("2000-01-01","2003-04-12"))
#' get_age(c("2000-01-01","2003-04-12"),dec = TRUE)
get_age <- function(from_date,to_date = lubridate::now(),dec = FALSE){
if(is.character(from_date)) from_date <- lubridate::as_date(from_date)
if(is.character(to_date)) to_date <- lubridate::as_date(to_date)
if (dec) { age <- lubridate::interval(start = from_date, end = to_date)/(lubridate::days(365)+lubridate::hours(6))
} else { age <- lubridate::year(lubridate::as.period(lubridate::interval(start = from_date, end = to_date)))}
age
}
if(dec)
中使用 /dyears(1)
?年龄不更像是一个时间段吗?应该使用 /years(1)
才更合适吧? - tjebo(lubridate :: days(365)+ lubridate :: hours(6))
。 - moodymudskipperdays()
和 hours()
结合起来。请问为什么选择 hours(6)
?啊,糟了,我想我明白了。把它转换成小数。不过这样能处理闰年吗? - tjebodyears
的作用有了错误的理解。尝试:lubridate::interval(start = "2000-01-01", end = "2001-01-01")/(lubridate::days(365)+lubridate::hours(6))
然后lubridate::interval(start = "2000-03-01", end = "2001-03-01")/(lubridate::days(365)+lubridate::hours(6))
。 - moodymudskipper> library(lubridate)
我认为/1931不是一个常见的日期类,因此我假设所有条目都是字符字符串。
> RECENT <- data.frame(recent = c("09/08/2005", "11/08/2005"))
> YEAR <- data.frame(year = c("/1931", "/1924"))
首先,让我们告诉R最近的日期是日期。我假设日期是按照月/日/年的顺序排列的,因此我使用mdy()。如果它们按照日/月/年的顺序排列,只需使用dmy()。
> RECENT$recent <- mdy(RECENT$recent)
recent
1 2005-09-08
2 2005-11-08
> YEAR$year <- as.numeric(substr(YEAR$year, 2, 5))
> year(RECENT$recent) - YEAR
year
1 74
2 81
如果你的年份实际上是完整的日期,你可以使用以下代码获取年份差:
> YEAR1 <- data.frame(year = mdy("01/08/1931","01/08/1924"))
> as.period(RECENT$recent - YEAR1$year, units = "year")
[1] 74 years and 8 months 81 years and 10 months
as.period(..., unit = "years")
而不是units = "year"
。 - user10915156您可以进行一些格式化操作:
as.numeric(format(as.Date("01/01/2010", format="%m/%d/%Y"), format="%Y")) - 1930
使用您的数据:
> yr <- c(1931, 1924)
> recent <- c("09/08/2005", "11/08/2005")
> as.numeric(format(as.Date(recent, format="%m/%d/%Y"), format="%Y")) - yr
[1] 74 81
由于你的数据在一个数据框中(我假设它叫做df
),所以代码应该更像这样:
as.numeric(format(as.Date(df$recent, format="%m/%d/%Y"), format="%Y")) - df$year
考虑您提供的数据示例:
> m <- data.frame(YEAR=c("/1931", "/1924"),RECENT=c("09/08/2005","11/08/2005"))
> m
YEAR RECENT
1 /1931 09/08/2005
2 /1924 11/08/2005
使用 strptime
函数提取年份:
> strptime(m[,2], format = "%m/%d/%Y")$year - strptime(m[,1], format = "/%Y")$year
[1] 74 81
> foo=data.frame(RECENT=c("09/08/2005","11/08/2005"),YEAR=c("/1931","/1924"))
> foo
RECENT YEAR
1 09/08/2005 /1931
2 11/08/2005 /1924
> foo$RECENTd = as.Date(foo$RECENT, format="%m/%d/%Y")
> foo$YEARn = as.numeric(substr(foo$YEAR,2,999))
> foo$AGE = as.numeric(format(foo$RECENTd,"%Y")) - foo$YEARn
> foo
RECENT YEAR RECENTd YEARn AGE
1 09/08/2005 /1931 2005-09-08 1931 74
2 11/08/2005 /1924 2005-11-08 1924 81
请注意,我假设您的年份列中有那个斜杠。
此外,在询问日期相关问题时的提示是包括一个超过12号的日期,这样我们就知道您是按照月/日/年还是日/月/年的格式。
as.numeric(as.Date("2002-02-02") - as.Date("1924-08-03")) / 365
给出输出:
77.55342
非常实用的方法,还支持使用lubridate
包的向量:
age <- function(date.birth, date.ref = Sys.Date()) {
if (length(date.birth) > 1 & length(date.ref) == 1) {
date.ref <- rep(date.ref, length(date.birth))
}
date.birth.monthdays <- paste0(month(date.birth), day(date.birth)) %>% as.integer()
date.ref.monthdays <- paste0(month(date.ref), day(date.ref)) %>% as.integer()
age.calc <- 0
for (i in 1:length(date.birth)) {
if (date.birth.monthdays[i] <= date.ref.monthdays[i]) {
# didn't had birthday
age.calc[i] <- year(date.ref[i]) - year(date.birth[i])
} else {
age.calc[i] <- year(date.ref[i]) - year(date.birth[i]) - 1
}
}
age.calc
}
这也包括闰年。我只是检查是否有人已经过生日了。