簡體   English   中英

如何計算時間間隔並將其除以r中的整數?

[英]how to calculate time interval and divide it by integer in r?

我有時間字符串為“08:00”,“06:00”,我想計算它們之間的差異,並將它除以15分鍾。

然后結果應該是8整數,我不知道如何在R中編碼

有人可以幫幫我嗎?

使用difftime這樣的東西?

difftime(
    as.POSIXct("08:00", format = "%H:%M"), 
    as.POSIXct("06:00", format = "%H:%M"), 
    units = "mins") / 15
#Time difference of 8 mins

或轉換為numeric

as.numeric(
    difftime(as.POSIXct("08:00", format = "%H:%M"), 
    as.POSIXct("06:00", format = "%H:%M"), 
    units = "mins") / 15)
#[1] 8

使用lubridate會很容易,我們將hm格式的字符串轉換為15分鍾。

library(lubridate)
(hm(a) - hm(b))/minutes(15)
#[1] 8

數據

a <- "08:00"
b <- "06:00"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM