将两组两列融合成两行(一组中的每一列对应一行)

Melting two sets of two columns into two rows (one row for each column in the set)

我有一个data.table如下:

DT <- fread(
"ID country year Event_A Event_B Event_A_succ Event_B_succ
4   NLD   2002  0   1   0   0
5   NLD   2002  0   1   0   1
6   NLD   2006  1   1   1   1
7   NLD   2006  1   0   1   0
8   NLD   2006  1   1   0   0
9   GBR   2002  0   1   0   0
10  GBR   2002  0   0   0   0
11  GBR   2002  0   1   0   1
12  GBR   2006  1   1   1   1
13  GBR   2006  1   1   0   1",
header = TRUE)

我想将 Event_XEvent_X_succ 列投射到行上而不对它们求和,从而创建新行。

我了解到 ,我可以按如下方式为 Event_AEvent_B 执行此操作:

library(data.table)
melt(DT, id.var = setdiff(names(DT), c("Event_A", "Event_B")), 
          value.name = 'Event')[, variable := NULL][order(ID)]

或(@IceCreamToucan):

melt(DT, measure.vars = c("Event_A", "Event_B"), value.name = 'Event')[, variable := NULL][order(ID)]

但我想添加第二个值名称 Event_succ 基于其他两列 Event_A_SuccEvent_B_Succes 并以相同的方式传播它们。

期望的输出:

DT <- fread(
"ID country year Event Event_succ
4   NLD   2002  0   0
4   NLD   2002  1   0
5   NLD   2002  0   0
5   NLD   2002  1   1
6   NLD   2006  1   1
6   NLD   2006  1   1
7   NLD   2006  1   1
7   NLD   2006  0   0
8   NLD   2006  1   0
8   NLD   2006  0   0
9   GBR   2002  1   0 
9   GBR   2002  1   0
10  GBR   2002  0   0
10  GBR   2002  0   0
11  GBR   2002  0   0
12  GBR   2002  1   0
13  GBR   2006  1   1
14  GBR   2006  1   1
15  GBR   2006  1   0
16  GBR   2006  1   1",
header = TRUE)

我该如何解决?

我们可以使用 measurepatterns

melt(DT, measure = patterns("Event_[AB]$", "Event_[AB]_succ"), 
     value.name =  c("Event", "Event_succ"))[, variable :=  NULL][order(ID)]
#    ID country year Event Event_succ
# 1:  4     NLD 2002     0          0
# 2:  4     NLD 2002     1          0
# 3:  5     NLD 2002     0          0
# 4:  5     NLD 2002     1          1
# 5:  6     NLD 2006     1          1
# 6:  6     NLD 2006     1          1
# 7:  7     NLD 2006     1          1
# 8:  7     NLD 2006     0          0
# 9:  8     NLD 2006     1          0
#10:  8     NLD 2006     1          0
#11:  9     GBR 2002     0          0
#12:  9     GBR 2002     1          0
#13: 10     GBR 2002     0          0
#14: 10     GBR 2002     0          0
#15: 11     GBR 2002     0          0
#16: 11     GBR 2002     1          1
#17: 12     GBR 2006     1          1
#18: 12     GBR 2006     1          1
#19: 13     GBR 2006     1          0
#20: 13     GBR 2006     1          1