0

Quite simply I want to calculate tenure, if an employee is termed, then I want the new field to return Term. Date - Job.Entry.Date to calculate tenure. Otherwise, if the employee is active, then I want the new field to return Sys.Date - Job.Entry.Date.

With the following Code:

jobentrydat_alltechs$Tenure <-if (jobentrydat_alltechs$Term.Date == is.na) {
  return (difftime(Sys.Date(), jobentrydat_alltechs$Job.Entry.Date, units = c("days")))
} else if (jobentrydat_alltechs$Term.Date != is.na) {
    return (difftime(jobentrydat_alltechs$Term.Date, jobentrydat_alltechs$Job.Entry.Date, units = c("days")))
}

I am getting the following error

Error in ==.default(jobentrydat_alltechs$Term.Date, is.na) :
comparison (1) is possible only for atomic and list types

lmo
  • 37,904
  • 9
  • 56
  • 69
J Walt
  • 141
  • 1
  • 9
  • Please supply a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Val Jun 14 '17 at 14:43

1 Answers1

1

You want ifelse instead of if, as it's vectorized, and also is.na is a function, not a value to be compared with.

jobentrydat_alltechs$Tenure <- ifelse(is.na(jobentrydat_alltechs$Term.Date),
  difftime(Sys.Date(), jobentrydat_alltechs$Job.Entry.Date, units = c("days")),
  difftime(jobentrydat_alltechs$Term.Date, jobentrydat_alltechs$Job.Entry.Date, units = c("days")))
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • I attempted it as you have with both ifelse and is.na(jobentrydat_alltechs$Term.Date) but was getting another error. I see my true error was beginning with an if, then ifelse as opposed to ifelse. Thanks. – J Walt Jun 14 '17 at 14:49