如何在 R 中创建这个变量? [英] How do I create this variable in R?
问题描述
考虑以下使用 R 的测试数据集:
Consider the following test data set using R:
testdat<-data.frame("id"=c(rep(1,5),rep(2,5),rep(3,5)),
"period"=rep(seq(1:5),3),
"treat"=c(c(0,1,1,1,0),c(0,0,1,1,1),c(0,0,1,1,1)),
"state"=c(rep(0,5),c(0,1,1,1,1),c(0,0,0,1,1)),
"int"=c(rep(0,13),1,1))
testdat
id period treat state int
1 1 1 0 0 0
2 1 2 1 0 0
3 1 3 1 0 0
4 1 4 1 0 0
5 1 5 0 0 0
6 2 1 0 0 0
7 2 2 0 1 0
8 2 3 1 1 0
9 2 4 1 1 0
10 2 5 1 1 0
11 3 1 0 0 0
12 3 2 0 0 0
13 3 3 1 0 0
14 3 4 1 1 1
15 3 5 1 1 1
前 4 个变量是我所拥有的,int
是我想要创建的变量.它类似于 treat
和 state
之间的交互,但会在第 8-10 行中包含 1,这是不希望的.本质上,我只希望在 treat
期间 state
发生变化时进行交互,否则不想要.关于如何创建此内容的任何想法(尤其是对于具有一百万个观测值的大规模数据集)?
The first 4 variables are what I have, int
is the variable I want to make. It is similar to an interaction between treat
and state
, but that would include 1s in rows 8-10 which is not desired. Essentially, I only want an interaction when state
changes during treat
but not otherwise. Any thoughts on how to create this (especially on a large scale for a dataset with a million observations)?
为了澄清为什么我想要这个措施.我想运行如下回归:
For clarification on why I want this measure. I want to run something like the following regression:
lm(outcome~treat+state+I(treat*state))
但只有当 treat
跨越 state
的变化时,我才真正对交互感兴趣.如果我要运行上述回归,I(treat*state)
会汇集我感兴趣的交互的影响,当 treat
完全为 1 时,state
是 1.理论上,我认为这些会有两种不同的效果,所以我需要将它们分解.我希望这是有道理的,我很乐意提供更多详细信息.
But I'm really interested in the interaction only when treat
straddles a change in state
. If I were to run the above regression, I(treat*state)
pools the effect of the interaction I'm interested in and when treat
is 1 entirely when state
is 1. In theory, I think these will have two different effects so I need to disaggregate them. I hope this makes sense and I am happy to provide additional details.
推荐答案
我确信这在基础 R 中是可能的,但这里有一个 tidyversion:
I'm sure this is possible in base R, but here's a tidyversion:
library(dplyr)
testdat %>%
group_by(grp = cumsum(c(FALSE, diff(treat) > 0))) %>%
mutate(int2 = +(state > 0 & first(state) == 0 & treat > 0)) %>%
ungroup() %>%
select(-grp)
# # A tibble: 15 x 6
# id period treat state int int2
# <dbl> <int> <dbl> <dbl> <dbl> <int>
# 1 1 1 0 0 0 0
# 2 1 2 1 0 0 0
# 3 1 3 1 0 0 0
# 4 1 4 1 0 0 0
# 5 1 5 0 0 0 0
# 6 2 1 0 0 0 0
# 7 2 2 0 1 0 0
# 8 2 3 1 1 0 0
# 9 2 4 1 1 0 0
# 10 2 5 1 1 0 0
# 11 3 1 0 0 0 0
# 12 3 2 0 0 0 0
# 13 3 3 1 0 0 0
# 14 3 4 1 1 1 1
# 15 3 5 1 1 1 1
分组的替代逻辑使用游程编码,实际上相同(建议您https://stackoverflow.com/a/35313426):
Alternative logic for grouping uses run-length encoding, effectively the same (suggested you https://stackoverflow.com/a/35313426):
testdat %>%
group_by(grp = { yy <- rle(treat); rep(seq_along(yy$lengths), yy$lengths); }) %>%
# ...
和那个答案一样,我希望 dplyr
有一个等效于 data.table
的 rleid
.预期的逻辑是能够按列中的连续相同值进行分组,但不是所有行中的值都相同.如果你看看这个中间管道(在清理 grp
之前),你会看到
And as in that answer, I wish dplyr
had an equivalent to data.table
's rleid
. The expected logic is to be able to group by consecutive same-values in a column, but not the same value across all rows. If you look at this mid-pipe (before cleaning up grp
), you'd see
testdat %>%
group_by(grp = { yy <- rle(treat); rep(seq_along(yy$lengths), yy$lengths); }) %>%
mutate(int2 = +(state > 0 & first(state) == 0 & treat > 0)) %>%
ungroup()
# # A tibble: 15 x 7
# id period treat state int grp int2
# <dbl> <int> <dbl> <dbl> <dbl> <int> <int>
# 1 1 1 0 0 0 1 0
# 2 1 2 1 0 0 2 0
# 3 1 3 1 0 0 2 0
# 4 1 4 1 0 0 2 0
# 5 1 5 0 0 0 3 0
# 6 2 1 0 0 0 3 0
# 7 2 2 0 1 0 3 0
# 8 2 3 1 1 0 4 0
# 9 2 4 1 1 0 4 0
# 10 2 5 1 1 0 4 0
# 11 3 1 0 0 0 5 0
# 12 3 2 0 0 0 5 0
# 13 3 3 1 0 0 6 0
# 14 3 4 1 1 1 6 1
# 15 3 5 1 1 1 6 1
但这只是一厢情愿.我想我也可以做
But that's just wishful thinking. I guess I could also do
my_rleid <- function(x) { yy <- rle(x); rep(seq_along(yy$lengths), yy$lengths); }
testdat %>%
group_by(grp = my_rleid(treat)) %>%
# ...
这篇关于如何在 R 中创建这个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!