使用 dplyr 和 lubridate 的 R 中两个日期之间的天数差异?
Difference in days between two dates in R using dplyr and lubridate?
想在 R 中实现 SQL 等同于 datediff 的功能?
基本上,我想在 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
首先将您的日期更改为润滑日期:
2022-01-05 将是
date1 <- ymd("2022-01-05")
date2 <- ymd("2022-01-07")
diff_days <- difftime(date2, date1, units="days")
想在 R 中实现 SQL 等同于 datediff 的功能?
基本上,我想在 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
首先将您的日期更改为润滑日期: 2022-01-05 将是
date1 <- ymd("2022-01-05")
date2 <- ymd("2022-01-07")
diff_days <- difftime(date2, date1, units="days")