修改问题的措辞并添加玩具数据框。
我正在尝试为地区级别的面板数据框创建一个time to treatment变量。数据帧是用每季度观察到的时间构建的。治疗时间变量应将治疗前的季度数显示为负数(...,-3,-2,-1),将治疗的季度数显示为0,并将治疗后的季度数显示为1,2,3,...对于每个地区/季度的观察值。示例数据帧:
df <- structure(list(District = c("Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye",
"Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad"),
YearQuarter = as.yearqtr(c("2011 Q1", "2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", "2012 Q4",
"2011 Q1", "2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", "2012 Q4")),
date = as_date(c("2011-01-01", "2011-04-02", "2011-07-01", "2011-10-01", "2012-01-01", "2012-04-01", "2012-07-01", "2012-10-01",
"2011-01-01", "2011-04-02", "2011-07-01", "2011-10-01", "2012-01-01", "2012-04-01", "2012-07-01", "2012-10-01")),
conflict = c(0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0),
timetreatment = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0),
basepresent = c(0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1)), class = "data.frame",
row.names = c(NA, -16L))时间处理变量等于1,区域处理开始的季度,否则为0。
我想要的是在可变时间处理中反映的,在第二个玩具数据框中显示。
df1 <- structure(list(District = c("Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye",
"Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad"),
YearQuarter = as.yearqtr(c("2011 Q1", "2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", "2012 Q4",
"2011 Q1", "2011 Q2", "2011 Q3", "2011 Q4", "2012 Q1", "2012 Q2", "2012 Q3", "2012 Q4")),
date = as_date(c("2011-01-01", "2011-04-02", "2011-07-01", "2011-10-01", "2012-01-01", "2012-04-01", "2012-07-01", "2012-10-01",
"2011-01-01", "2011-04-02", "2011-07-01", "2011-10-01", "2012-01-01", "2012-04-01", "2012-07-01", "2012-10-01")),
conflict = c(0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0),
timetreatment = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0),
basepresent = c(0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1),
timetotreat = c(-3, -2, -1, 0, 1, 2, 3, 4, -4, -3, -2, -1, 0, 1, 2, 3)), class = "data.frame",
row.names = c(NA, -16L))创建timetotreatment变量所需转换的当前代码为
df <- df%>% mutate(timetotreatment = YearQuarter - YearQuarter[timetreatment == 1])
df <- df%>% mutate(timetotreatment = date - date[timetreatment == 1])这两个返回值都是关闭的,即timetreatment =0的季度不返回timetotreatment=0。
> dput(head(df, 20))
structure(list(District = c("Afgooye", "Afgooye", "Afgooye",
"Afgooye", "Afgooye", "Afgooye", "Afgooye", "Afgooye", "Balcad",
"Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad", "Balcad"
), YearQuarter = structure(c(2011, 2011.25, 2011.5, 2011.75,
2012, 2012.25, 2012.5, 2012.75, 2011, 2011.25, 2011.5, 2011.75,
2012, 2012.25, 2012.5, 2012.75), class = "yearqtr"), date = structure(c(14975,
15066, 15156, 15248, 15340, 15431, 15522, 15614, 14975, 15066,
15156, 15248, 15340, 15431, 15522, 15614), class = "Date"), conflict = c(0,
0, 1, 0, 0, 4, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0), timetreatment = c(0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), basepresent = c(0,
0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1), timetotreatment = c(-0.75,
-0.75, -0.25, -0.25, 0.25, 0.25, 0.75, 0.75, -0.75, -0.75, -0.25,
-0.25, 0.25, 0.25, 0.75, 0.75), timetotreatment1 = structure(c(-273,
-274, -92, -92, 92, 91, 274, 274, -273, -274, -92, -92, 92, 91,
274, 274), class = "difftime", units = "days")), row.names = c(NA,
16L), class = "data.frame")发布于 2020-11-28 23:31:03
使用tidyverse和data.table的游程长度id (rleid)函数,我们可以根据观察所处的分段进行分组,即rleid==1 =>预处理,rleid==2 =>处理,rleid==3后处理:
library(tidyverse)
df %>%
group_by(District) %>%
mutate(t=data.table::rleid(timetreatment)) %>%
group_by(t, .add=T) %>%
mutate(timetotreat=- (t==1) * (n()+1) + row_number() - 1*(t==2)) %>%
select(-t) t District YearQuarter date conflict timetreatment basepresent timetotreat
<int> <chr> <yearqtr> <date> <dbl> <dbl> <dbl> <dbl>
1 1 Afgooye 2011 Q1 2011-01-01 0 0 0 -3
2 1 Afgooye 2011 Q2 2011-04-02 0 0 0 -2
3 1 Afgooye 2011 Q3 2011-07-01 1 0 0 -1
4 2 Afgooye 2011 Q4 2011-10-01 0 1 1 0
5 3 Afgooye 2012 Q1 2012-01-01 0 0 1 1
6 3 Afgooye 2012 Q2 2012-04-01 4 0 1 2
7 3 Afgooye 2012 Q3 2012-07-01 0 0 1 3
8 3 Afgooye 2012 Q4 2012-10-01 0 0 1 4
9 1 Balcad 2011 Q1 2011-01-01 0 0 0 -4
10 1 Balcad 2011 Q2 2011-04-02 0 0 0 -3
11 1 Balcad 2011 Q3 2011-07-01 0 0 0 -2
12 1 Balcad 2011 Q4 2011-10-01 2 0 0 -1
13 2 Balcad 2012 Q1 2012-01-01 0 1 0 0
14 3 Balcad 2012 Q2 2012-04-01 0 0 1 1
15 3 Balcad 2012 Q3 2012-07-01 1 0 1 2
16 3 Balcad 2012 Q4 2012-10-01 0 0 1 3https://stackoverflow.com/questions/65045281
复制相似问题