0

So I want to check if the age and the birthyear are matching. What is the easiest way to do that?

I think I can just add these two together and check if it is 2021.

But how would I just exlude these rows that don't match?

nero7
  • 1
  • 3
  • Please provide a reproducible example (preferably using `dput`) and thus we can provide a concrete answer. – Mossa Nov 08 '21 at 13:14
  • Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – NelsonGon Nov 08 '21 at 13:15

1 Answers1

0

You could define a little function that takes the date of birth as input and returns the actual age in years as an output:

get_age <- function(DOB) {
  floor(lubridate::interval(DOB, lubridate::now())/lubridate::years(1))
}

For example, suppose you had a data frame of DOB and ages:

df <- data.frame(DOB = as.Date(c("1984-07-01", "1970-09-22", 
                                 "2015-09-11", "1999-05-03")),
                 age = c(37, 51, 16, 22))

df
#>          DOB age
#> 1 1984-07-01  37
#> 2 1970-09-22  51
#> 3 2015-09-11  16
#> 4 1999-05-03  22

Now you want to see if the recorded ages match the actual age as calculated by the date of birth. We can add a column giving the calculated age like this:

df$real_age <- get_age(df$DOB)

df
#>          DOB age real_age
#> 1 1984-07-01  37       37
#> 2 1970-09-22  51       51
#> 3 2015-09-11  16        6
#> 4 1999-05-03  22       22

Finally, we can filter the data frame so that we only keep the rows with the correct age.

df[df$age == df$real_age,]
#>          DOB age real_age
#> 1 1984-07-01  37       37
#> 2 1970-09-22  51       51
#> 4 1999-05-03  22       22
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Sounds great, but it doesn't work for me. I only have the birthyear not the exact year and date. And it is coded as factor. So don't know.. – nero7 Nov 08 '21 at 13:37
  • @nero7 that's why we normally ask for a reproducible example of your data. Otherwise, we need to assume the exact format of what you have. You could convert the birth year to a number using `as.numeric(as.character(birth_year))`. The age plus the birth year should add up to 2021 or to 2020, depending on when their birthday is. – Allan Cameron Nov 08 '21 at 13:44