我想将Date转换为顺序分析的因子。
我尝试了以下代码:
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)
但我得到以下输出:
相反,我希望以下输出:
谢谢您的帮助 !!
为什么不使用:
as.Date(trans_sequence$repdate)-as.Date('2019-01-01')
?
例:
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