-3

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 !!

Community
  • 1
  • 1
  • 1
    Can you show`tran_sequence$repdate` using `dput` `dput(head(trans_sequence$repdate))` – akrun Mar 08 '19 at 14:40
  • structure(c(47L, 56L, 29L, 59L, 66L, 46L), .Label = c("2019-01-02", "2019-01-03", "2019-01-04", "2019-01-05", "2019-01-06", "2019-01-07", "2019-01-08", "2019-01-09", "2019-01-10", "2019-01-11", "2019-01-12", "2019-01-13", "2019-01-14", "2019-01-15", "2019-01-16", "2019-01-17", etc..... – Dominik Raab Mar 08 '19 at 14:46
  • 1
    Please edit your **question** according to [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to help future viewers(you too). – NelsonGon Mar 08 '19 at 15:14

1 Answers1

1

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
iod
  • 7,412
  • 2
  • 17
  • 36