簡體   English   中英

使用R中lubridate包中的`difftime`函數來查找日期向量中的連續差異?

[英]Using the `difftime` function within the lubridate package in R to find consecutive differences in a vector of dates?

我目前有一個稱為dates的日期向量,該日期已使用ymd_hms函數獲取。

dates <- c("2013-09-01 23:05:24", "2013-09-02 22:51:09", "2013-09-04 00:09:16", "2013-09-04 23:43:31", "2013-09-06 00:17:41", "2013-09-06 22:42:27", "2013-09-08 00:22:27")

ymd_hms(dates)

我試圖找到日期之間的連續差異,最容易使用的功能是difftime 我可以在R中使用某個功能或應用功能來快速執行此操作嗎? 盡管上面的內容很簡短,但可能很容易,但我正在嘗試提出一種解決方案,該方案可以處理數億行。

通常我會這樣做:

as.numeric(diff(event_date)))

但我需要在difftime中設置日期,秒數等。

謝謝!

您可能沒有意識到ymd_hms(dates)不會更改'dates'。 diff不需要任何功能。 (不確定difftime是什么。) diff是向量化的。

dates <- c("2013-09-01 23:05:24", "2013-09-02 22:51:09", "2013-09-04 00:09:16", "2013-09-04 23:43:31", "2013-09-06 00:17:41", "2013-09-06 22:42:27", "2013-09-08 00:22:27")

realdates <-ymd_hms(dates)
diff(realdates)

暫無
暫無

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

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