-3
  • I have some data table which looks like this. [DATA] Customer ID : 1255455 , 5258545 , 227484, 25485418 , 3745648 ..... SEX : 1 , 1 , 2, 1 , 2 ..... JOIN_YM : 200608 , 201802 ,20170408, 201104 , 201009 .....

    I just want to calculate the time difference between "2019/12/30" and the variables "JOIN_YM" In conclusion, I want to calculate the time spanned.

    Could you help me how to calculate this for using R programming.

hyejuryu
  • 109
  • 4

1 Answers1

1

Overall, calculating a timedifference can be obtained using difftime function (you can change the unit to your liking) . For parsing dates, I would suggest the lubridate package.

data <- data.frame(JOIN_YN="201802")
data %>% 
  mutate(
    timeSpanned = difftime(
      lubridate::ymd("2019-12-30"), 
      lubridate::ymd(paste0(JOIN_YN, "01")), 
      units = "days"
      )
    )

Jeroen Colin
  • 345
  • 1
  • 6