rdatesequentialfactorsevent-id

Convert Date into Factors for sequential analysis


I want to convert Date into factor for sequential analysis. repdate should be converted into factor for further analysis

I tried the following code:

start_month <- '2019-01-01'



elapsed_month <- function(end_date, start_date) {
ed <- as.POSIXlt(end_date)
sd <- as.POSIXlt(start_date)
12 * (ed$year - sd$year) + (ed$mon - sd$mon)
}

trans_sequence$eventID <- elapsed_month(trans_sequence$repdate, 
start_month)

But i got the following output: eventID

Instead I wish following output: enter image description here

Thank You for your Help !!


Solution

  • Why not just use:

    as.Date(trans_sequence$repdate)-as.Date('2019-01-01')
    

    ?

    Example:

    cc<-as.Date(c("2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06", "2019-01-07", "2019-01-08"))
    cc
     [1] "2019-01-02" "2019-01-03" "2019-01-04" "2019-01-05"
     [5] "2019-01-06" "2019-01-07" "2019-01-08"
    cc-as.Date('2019-01-01')
     Time differences in days
     [1] 1 2 3 4 5 6 7