Negative age in R

2

I have a database that needs to calculate the age of the members in it, below a sample extracted with dput .

base.teste <- c("04/03/73", "10/09/67", "21/12/74", "17/04/76", "25/03/66", 
"11/03/73", "06/08/79")

I need to calculate the age of all members by the end of November, 11/30/2017. I transofmrei the data in dates, lubridate % and executed the following code:

library(lubridate)
idade <- floor(as.numeric(difftime(data.fim.mes, base.teste, units = "days"))/365.25)

But in the result I end up getting negative ages:

44 -50  42  41 -49  44  38

Does anyone know why or have a better way of calculating?

    
asked by anonymous 18.01.2018 / 17:38

1 answer

4

A solution using the package chron

library(chron)
base.teste <- c("04/03/73", "10/09/67", "21/12/74", "17/04/76", "25/03/66", "11/03/73", "06/08/79")
base.teste <- chron(base.teste, format = c(dates = "d/m/y")) 
data.fim.mes <- "30/11/17"
data.fim.mes <- chron(data.fim.mes, format = c(dates = "d/m/y"))

idade <- floor(as.numeric(difftime(data.fim.mes, base.teste, units = "days"))/365.25)

results in:

idade
[1] 44 50 42 41 51 44 38
    
18.01.2018 / 18:13