1

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!

Avenn
  • 95
  • 1
  • 7
  • Sample data would be incredibly useful here, perhaps `dput(head(retiredonly))` and the value you expect from this sample data. – r2evans Aug 12 '19 at 17:34
  • @r2evans wasn't really sure how dput would be used, but I added dummy data and explained the answer I'd hope to get. – Avenn Aug 12 '19 at 17:46
  • 1
    `dput` provide unambiguous data, and we can use it "immediately" without guessing. Your data is *wrong*, unfortunately, please be careful to provide code you are using. In this case, `1982-02-02` resolves to `1978` because R sees it as subtraction. (Or is that really your problem?) Providing programmatic fake data is good, too, so if it's just a matter of that, then add quotes. – r2evans Aug 12 '19 at 17:53
  • For "why" to use `dput`, please take a look at https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Aug 12 '19 at 17:54
  • `transform(test, retire_date = hire_date + (79*365.25 - (hire_date - birth_date))/2)`? – r2evans Aug 12 '19 at 18:06
  • I appreciate the explanation of dput. I can't use it here, because the real data contains confidential information, but I'll remember them for next time. I fixed the dummy dataset so that it is in the right format. – Avenn Aug 12 '19 at 18:08
  • @r2evans that worked! It isn't the way I had it originally done, but that code disappeared into the abyss so if it works, it works. Appreciate the help! – Avenn Aug 12 '19 at 18:11
  • I understand, sensitive data is always hard to ask questions over. Programmatic data-building with `data.frame` (etc) is just as good, the key being providing unambiguous data (what is showed on the console can easily be different from what is internally stored). – r2evans Aug 12 '19 at 18:12

1 Answers1

2
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
r2evans
  • 141,215
  • 6
  • 77
  • 149