[英]Difference in days between two dates in R using dplyr and lubridate?
想要在 R 中执行与 datediff 等效的 SQL 吗?
基本上,我想在 R 中进行这个计算
Delivery Date Expected Date Difference
2022-01-05 2022-01-07 -2
将列转换为Date
class 并使用difftime
df1$Difference <- with(df1, as.numeric(difftime(as.Date(DeliveryDate),
as.Date(ExpectedDate), units = "days")))
或使用tidyverse
library(dplyr)
library(lubridate)
df1 %>%
mutate(Difference = as.numeric(difftime(ymd(DeliveryDate),
ymd(ExpectedDate), units = "days")))
DeliveryDate ExpectedDate Difference
1 2022-01-05 2022-01-07 -2
首先将您的日期更改为 lubridate date: 2022-01-05 将是
date1 <- ymd("2022-01-05")
date2 <- ymd("2022-01-07")
diff_days <- difftime(date2, date1, units="days")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.