Date-time differences between rows in R

后端 未结 2 2112
不思量自难忘°
不思量自难忘° 2020-12-06 17:44

I would like to calculate time differences (delta time) in R. The timestamps are stored in a two-column data frame with time as date-time (year-month-day hour:min: sec.msec)

2条回答
  •  春和景丽
    2020-12-06 18:32

    I will not give you the entire answer, but this will help you almost get there:

    x="2012-07-11 04:22:40.169"
    datex=strptime(x,format='%Y-%m-%d %H:%M:%S') #this converts your date string 
    #into a date value recognized in r
    
    y="2012-08-15 08:32:40.169"
    datey=strptime(y,format='%Y-%m-%d %H:%M:%S')
    
    time_diff=as.numeric(difftime(datey,datex)) #in decimal days
    >35.17361
    

    From decimal days you can then convert it back to whichever time format you want, but depending on what you want to do with it, you may want to keep it in a numeric form (perhaps in decimal hours, by multiplying time_diff by 24)...

提交回复
热议问题