1

イベントが発生したかどうかを示すバイナリ変数を持つ不均衡なパネル データがあります。時間依存性を制御したいので、最後のイベントからの経過年数を示す変数を作成したいと考えています。データは 2 年ごとに編成されています。

これは、私が達成しようとしていることのベクトルを含む再現可能な例です。ありがとう!

   id year onset time_since_event
1   1 1989     0                1
2   1 1990     0                2
3   1 1991     1                0
4   1 1992     0                1
5   1 1993     0                2
6   2 1989     0                1
7   2 1990     1                0
8   2 1991     0                1
9   2 1992     1                0
10  3 1991     0                1
11  3 1992     0                2

˚

id <- c(1,1,1,1,1,2,2,2,2,3,3)
year <- c(1989,1990,1991,1992,1993,1989,1990,1991,1992,1991,1992)
onset <- c(0,0,1,0,0,0,1,0,1,0,0)
time_since_event<-c(1,2,0,1,2,1,0,1,0,1,2) #what I want to create
df <- data.frame(cbind(id, year, onset,time_since_event))
4

1 に答える 1

1

を使用できますdata.table。'data.frame' を 'data.table' (setDT(df)に変換し、 を使用して 'onset' 列に基づいてランレングス ID グループ化変数 ('ind') を作成します。'ind' 列rleidと 'id' 列でグループ化して、 'onset' が 1 に等しくない行シーケンスとしての 'time_since_event' 列。次のステップで、'NA' 要素を 0 に置き換えます。

library(data.table)#v1.9.6+
setDT(df)[, ind:=rleid(onset)][onset!=1, time_since_event:=1:.N , 
     by = .(ind, id)][is.na(time_since_event), time_since_event:= 0]

df
#     id year onset ind time_since_event
# 1:  1 1989     0   1                1
# 2:  1 1990     0   1                2
# 3:  1 1991     1   2                0
# 4:  1 1992     0   3                1
# 5:  1 1993     0   3                2
# 6:  2 1989     0   3                1
# 7:  2 1990     1   4                0
# 8:  2 1991     0   5                1
# 9:  2 1992     1   6                0
#10:  3 1991     0   7                1
#11:  3 1992     0   7                2

または、コンパクトにすることもできます。rleid(onset)「id」列でグループ化して、「onset」を否定し (0 が TRUE になり、1 FALSE になるように)、行シーケンス ( ) を乗算し、それを「time_since_event」列として1:.N割り当てます ( )。:=

setDT(df)[,time_since_event := 1:.N *!onset, by = .(rleid(onset), id)]
df
#    id year onset time_since_event
# 1:  1 1989     0                1
# 2:  1 1990     0                2
# 3:  1 1991     1                0
# 4:  1 1992     0                1
# 5:  1 1993     0                2
# 6:  2 1989     0                1
# 7:  2 1990     1                0
# 8:  2 1991     0                1
# 9:  2 1992     1                0
#10:  3 1991     0                1
#11:  3 1992     0                2

または、 を使用できますdplyr。「id」でグループ化し、別の変数を作成します (「onset」( diff) 内の隣接する要素の差をとって、論理インデックス ( !=0) とインデックスを作成cumsumします)。内でmutate、行シーケンス ( row_number()) を否定された 'onset' で乗算し (前と同様)、 を使用して 'ind' 列を削除しselectます。

library(dplyr)
df %>% 
    group_by(id, ind= cumsum(c(TRUE, diff(onset)!=0))) %>% 
    mutate(time_since_event= (!onset) *row_number()) %>%
    ungroup() %>%
    select(-ind) 
#     id  year onset time_since_event
#   (dbl) (dbl) (dbl)            (int)
#1      1  1989     0                1
#2      1  1990     0                2
#3      1  1991     1                0
#4      1  1992     0                1
#5      1  1993     0                2
#6      2  1989     0                1
#7      2  1990     1                0
#8      2  1991     0                1
#9      2  1992     1                0
#10     3  1991     0                1
#11     3  1992     0                2

データ

df <- data.frame(id, year, onset)
于 2015-10-31T15:17:57.073 に答える