How to make time difference in same units when subtracting POSIXct

后端 未结 1 1391
不思量自难忘°
不思量自难忘° 2020-12-03 07:38

I want to subtract to POSIXct. I can do this but depending on the first row (i guess?) the difference will be in seconds or minutes. Below you can see the first diff is in

相关标签:
1条回答
  • 2020-12-03 08:13

    You can use difftime for that propose which allows you to specify the measurement units, for example

    difftime(t1, t2, units = "secs")
    

    Another way (as mentioned by @nicola and is present in the same documentation) is to take advantage of the fact that - has a -.POSIXt method and override the measurement units after the subtraction operation using units<- replacement method

    res <- t1 - t2
    units(res) <- "secs"
    
    0 讨论(0)
提交回复
热议问题