0

I have data that result as active and inactive. I need to compute the service length using different formulas due to different end dates. The data example as follow:

status joindate leftdate  lengthOfservice
active 1/1/2020 <NA>
active 6/7/1999 <NA>
active 15/8/2015 <NA>
inactive 1/1/1984 <31/12/2009 

I set the date as

library("lubridate")

Start <- as.Date(joindate)
End1 <- as.Date(30/4/2022) # an example of given date
End2 <- as.Date(leftdate)

I code as follow: *for inactive status

time_of_length <- interval(start, end2) %>%
  as.numeric("years")

From this, i only have result for inactive person while Missing Value (NA) for active person.

I think the other one formula that should i use :

*for active

Time_of_length <- interval(start, end1) %>%
  as.numeric("years")

However, i really did not know how to combined this. Im new to R. Thank you for your assistance.

Please assist me how can i solve the probelm. Is it using if else and to group_by them?

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Azreen
  • 1
  • 1
  • 5
    Welcome to SO, Azreen! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Nov 21 '22 at 17:52
  • 2
    Some of this is not recognizable to me as syntactically valid `r` code. It will be hard to give more help without a reproducible example as indicated above. – Dan Adams Nov 21 '22 at 17:55

0 Answers0