我希望通過兩列和兩行的組來確定天數差異。基本上從結束日減去后續行中的后續開始日,并將差異記錄為資料框中的新列,并在識別新組 (ID) 時重新開始。
Start_Date End_Date ID
2014-05-09 2015-05-08 01
2015-05-09 2016-05-08 01
2016-05-11 2017-05-10 01
2017-05-11 2018-05-10 01
2016-08-29 2017-08-28 02
2017-08-29 2018-08-28 02
結果應該類似于下表。
Start_Date End_Date ID Days_Difference
2014-05-09 2015-05-08 01 NA
2015-05-09 2016-05-08 01 01
2016-05-11 2017-05-10 01 03
2017-05-11 2018-05-10 01 01
2016-08-29 2017-08-28 02 NA
2017-08-29 2018-08-28 02 01
本質上,我想計算結束日期與其左對角線開始日期跨組 (ID) 的差異。我真的很難過這個。我不認為我的代碼會有幫助。任何使用 tidyverse、data.table 或 base R 的解決方案都將不勝感激!
uj5u.com熱心網友回復:
分組后我們可能會得到lead'Start_Date'和'End_Date'的(下一個元素)之間的差異
library(dplyr)
df1 <- df1 %>%
mutate(across(ends_with("Date"), as.Date)) %>%
group_by(ID) %>%
mutate(Days_Difference = as.numeric(lag(lead(Start_Date) - End_Date))) %>%
ungroup
-輸出
df1
# A tibble: 6 × 4
Start_Date End_Date ID Days_Difference
<date> <date> <int> <dbl>
1 2014-05-09 2015-05-08 1 NA
2 2015-05-09 2016-05-08 1 1
3 2016-05-11 2017-05-10 1 3
4 2017-05-11 2018-05-10 1 1
5 2016-08-29 2017-08-28 2 NA
6 2017-08-29 2018-08-28 2 1
或者類似的邏輯data.table
library(data.table)
setDT(df1)[, Days_Difference :=
as.numeric(shift(shift(as.IDate(Start_Date), type = "lead") -
as.IDate(End_Date))), ID]
-輸出
> df1
Start_Date End_Date ID Days_Difference
<char> <char> <int> <num>
1: 2014-05-09 2015-05-08 1 NA
2: 2015-05-09 2016-05-08 1 1
3: 2016-05-11 2017-05-10 1 3
4: 2017-05-11 2018-05-10 1 1
5: 2016-08-29 2017-08-28 2 NA
6: 2017-08-29 2018-08-28 2 1
資料
df1 <- structure(list(Start_Date = c("2014-05-09", "2015-05-09", "2016-05-11",
"2017-05-11", "2016-08-29", "2017-08-29"), End_Date = c("2015-05-08",
"2016-05-08", "2017-05-10", "2018-05-10", "2017-08-28", "2018-08-28"
), ID = c(1L, 1L, 1L, 1L, 2L, 2L)), class = "data.frame",
row.names = c(NA,
-6L))
uj5u.com熱心網友回復:
另一種data.table選擇
setDT(df)[
,
c(lapply(.SD, as.IDate), .(ID = ID)),
.SDcols = patterns("Date$")
][
,
DayspDiff := Start_Date - shift(End_Date),
ID
][]
產量
Start_Date End_Date ID DayspDiff
1: 2014-05-09 2015-05-08 1 NA
2: 2015-05-09 2016-05-08 1 1
3: 2016-05-11 2017-05-10 1 3
4: 2017-05-11 2018-05-10 1 1
5: 2016-08-29 2017-08-28 2 NA
6: 2017-08-29 2018-08-28 2 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535811.html
