Reputation: 95
I am trying to solve for a date so that the parameter I am looking for is true in an ifelse statement, so that the true input is the date. I'm having difficulty wording what I'm trying to do, but basically I have when someone is hired, and when they were born. I need to find the date that how long they've been working + how old they are adds up to 79.
I've gone through a couple different ways of writing the ifelse code, but the error is in getting that placeholder variable to be solved for.
library(lubridate)
hire_date <- ymd("1982-02-02")
birth_date <- ymd("1967-02-02")
retire <- 1
test <- data.frame(hire_date, birth_date, retire)
test$pension4 <- ifelse(test$retire == 1, ifelse(((
as.duration(ymd(x) %--% test$hire_date / dyears(1))) + (as.duration(ymd(x)
%--% test$birth_date / dyears(1)))) == 79, x, NA), NA)
x is what I want to solve for. In the example, if my person was hired on 1982-02-02 and born 1967-02-02, what I want input into pension4 is the date where their combined age and years of service is 79. In the example, that would be 2014-02-02. They started working at 15 (I just made random numbers up so yeah that's not super accurate), so in 2014 they will be 47 and have worked for 32 years, for a combined age and years of service of 79.
If I need to use something other than ifelse, that's fine, I just need to be able to get that date.
Thank you!
Upvotes: 1
Views: 46
Reputation: 160417
test <- data.frame(hire_date = as.Date("1982-02-02"), birth_date = as.Date("1967-02-02"))
transform(test, retire_date = hire_date + (79*365.25 - (hire_date - birth_date))/2)
# hire_date birth_date retire_date
# 1 1982-02-02 1967-02-02 2014-02-02
Upvotes: 2