将两组两列融为两行(组中每一列一行) [英] Melting two sets of two columns into two rows (one row for each column in the set)

查看:116
本文介绍了将两组两列融为两行(组中每一列一行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table,如下:

I have a data.table as follows:

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_X Event_X_succ 列强制转换为该行,而不对其求和,以创建新行。

I want to cast the Event_X AND Event_X_succ columns over the row without summing them, creating new rows.

我了解了在这里,我可以在 Event_A 中执行此操作,并 Event_B 如下:

I learned here, that I can do this for Event_A and Event_B as follows:

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

或(@IceCreamToucan):

or (@IceCreamToucan):

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

但是我想添加第二个值名称, Event_succ 基于其他两个列,分别称为 Event_A_Succ Event_B_Succes ,并以相同的方式进行传播

But I would like to add a second value name, Event_succ based on the other two columns called Event_A_Succ and Event_B_Succes and to spread those in the same way .

所需的输出:

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)

我应该如何解决?

推荐答案

我们可以将度量模式

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

这篇关于将两组两列融为两行(组中每一列一行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆