R Find time difference in seconds for YYYY-MM-DD HH:MM:SS.MMM format

前端 未结 2 1740
南方客
南方客 2021-02-07 10:08

I\'m trying to subtract 2 character vectors containing date time information in the following format:

> dput(train2)

structure(list(time2 = c(\"2011-09-01 23         


        
2条回答
  •  悲&欢浪女
    2021-02-07 10:39

    >  x1<-"2013-03-03 23:26:46.315" 
    >  x2<-"2013-03-03 23:31:53.091"
    >  x1 <- strptime(x1, "%Y-%m-%d %H:%M:%OS")
    >  x2 <- strptime(x2, "%Y-%m-%d %H:%M:%OS")
    > x1
    [1] "2013-03-03 23:26:46"
    > x2
    [1] "2013-03-03 23:31:53"
    

    I followed the answer of @Dirk Eddelbuettel, but I am losing precision. How can I force R to not be cuting parts of second?

    Thankfully (man of strptime) I answered my question myself:

    op <- options(digits.secs = 3)
    

    After applying this setting the precision will be used.

    http://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html

    The belowe may be useful if you would like to get difference in seconds, but get in minutes:

    > as.numeric(x2-x1,units="secs")
    [1] 306.776
    

提交回复
热议问题