【问题标题】:Retaining column information when melting multiple columns into one将多列合并为一列时保留列信息
【发布时间】:2020-07-11 16:40:21
【问题描述】:

我有一个 data.table 我已经融化如下:

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

melt(DT, id.var = setdiff(names(DT), c("Event_A", "Event_B")), 
          value.name = 'Event')[, variable := NULL][order(ID)]
#     ID country year Event
# 1:  4     NLD 2002     0
# 2:  4     NLD 2002     1
# 3:  5     NLD 2002     0
# 4:  5     NLD 2002     1
# 5:  6     NLD 2006     1
# 6:  6     NLD 2006     1
# 7:  7     NLD 2006     1
# 8:  7     NLD 2006     0
# 9:  8     NLD 2006     1
#10:  8     NLD 2006     1
#11:  9     GBR 2002     0
#12:  9     GBR 2002     1
#13: 10     GBR 2002     0
#14: 10     GBR 2002     0
#15: 11     GBR 2002     0
#16: 11     GBR 2002     1
#17: 12     GBR 2006     1
#18: 12     GBR 2006     1
#19: 13     GBR 2006     1
#20: 13     GBR 2006     1

但是,事后看来,我希望在融化的数据集中拥有 Event 类别。如何确保这些信息保留在融合数据中?

编辑(由于原始帖子过于简化):

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

DT<- melt(DT, measure = patterns("^Event_", "^Choice_"), 
     value.name =  c("Event", "Choice"))[, variable :=  NULL][order(ID)]

期望的输出:

#     ID country year Event Event_Cat Choice Choice_Cat
# 1:  4     NLD 2002     0  A         0      A
# 2:  4     NLD 2002     1  B         1      B
# 3:  5     NLD 2002     0  A
# 4:  5     NLD 2002     1  B
# 5:  6     NLD 2006     1  A
# 6:  6     NLD 2006     1  B
# 7:  7     NLD 2006     1
# 8:  7     NLD 2006     0
# 9:  8     NLD 2006     1
#10:  8     NLD 2006     1
#11:  9     GBR 2002     0
#12:  9     GBR 2002     1
#13: 10     GBR 2002     0
#14: 10     GBR 2002     0
#15: 11     GBR 2002     0
#16: 11     GBR 2002     1
#17: 12     GBR 2006     1
#18: 12     GBR 2006     1
#19: 13     GBR 2006     1
#20: 13     GBR 2006     1

【问题讨论】:

  • 省略[, variable := NULL] ?
  • 请确保您提供一个最小的工作示例,以防止扩大讨论。

标签: r data.table melt


【解决方案1】:

不要NULLify variable.name

setnames(
   melt(DT, id.var = setdiff(names(DT), c("Event_A", "Event_B")), value.name = 'Event')[
     , variable:=sub("Event_", "", variable)][order(ID)], 
   old="variable", new="Event_Cat")

    ID country year Event_Cat Event
 1:  4     NLD 2002         A     0
 2:  4     NLD 2002         B     1
 3:  5     NLD 2002         A     0
 4:  5     NLD 2002         B     1
 5:  6     NLD 2006         A     1
 6:  6     NLD 2006         B     1 ...

编辑,基于提供的新信息(合并多个列)。

DT2 <- setnames(
  melt(DT, measure = patterns("^Event_", "^Choice_"), 
         value.name =  c("Event", "Choice"))[, variable := forcats::lvls_revalue(variable, 
            c("A", "B"))][order(ID)],
  old="variable", new="Cetegory")
DT2

    ID country year Cetegory Event Choice
 1:  4     NLD 2002        A     0      0
 2:  4     NLD 2002        B     1      1
 3:  5     NLD 2002        A     0      1
 4:  5     NLD 2002        B     1      1
 5:  6     NLD 2006        A     1      0
 6:  6     NLD 2006        B     1      1 ...

【讨论】:

  • 非常感谢@Edward!我有一个问题,我让我的例子有点简单。我实际上使用的是measure = patterns("^Event_", "^Choice_") 而不是c("Event_A", "Event_B")。我试图调整你的代码,但我不完全确定结束.. setnames( melt(DT, id.var = setdiff(names(DT), measure = patterns("^Event_", "^Choice_")), value.name = 'Event')[ , variable:=sub("Event_", "", variable)][order(ID)], old="variable", new="Event_Cat")
  • 请参阅编辑
  • 感谢您的评论爱德华,但我不确定我是否关注哈哈..
【解决方案2】:

您可以使用 pivot_longer 中的 tidyr

tidyr::pivot_longer(DT, cols = starts_with('Event'), 
                    names_to = c('.value', 'Event_Cat'), 
                    names_sep = '_')


#     ID country  year Event_Cat Event
#   <int> <chr>   <int> <chr>     <int>
# 1     4 NLD      2002 A             0
# 2     4 NLD      2002 B             1
# 3     5 NLD      2002 A             0
# 4     5 NLD      2002 B             1
# 5     6 NLD      2006 A             1
# 6     6 NLD      2006 B             1
# 7     7 NLD      2006 A             1
# 8     7 NLD      2006 B             0
# 9     8 NLD      2006 A             1
#10     8 NLD      2006 B             1
#11     9 GBR      2002 A             0
#12     9 GBR      2002 B             1
#13    10 GBR      2002 A             0
#14    10 GBR      2002 B             0
#15    11 GBR      2002 A             0
#16    11 GBR      2002 B             1
#17    12 GBR      2006 A             1
#18    12 GBR      2006 B             1
#19    13 GBR      2006 A             1
#20    13 GBR      2006 B             1

【讨论】:

  • 感谢您的回答!我在实际数据中使用measure = patterns("^Event_", "^Choice_") 而不是c("Event_A", "Event_B")starts_with 相当于什么?我试过starts_with(c('ECOST', 'Choice'),但没用。
  • @Tom 使用matches 而不是starts_with, matches('^Event|^Choice')
  • 谢谢,这很遗憾地在实际数据上抛出了一些错误。
  • @Tom 在您更新的数据上也为我工作,没有任何错误。 tidyr::pivot_longer(DT, cols = matches('^Event|^Choice'), names_to = c('.value', 'Event_Cat'), names_sep = '_')
  • 我不知何故收到了这个警告:Warning: Expected 2 pieces. Missing pieces filled with NA in 2 rows [1, 2],这使得Event_Cat NA。
猜你喜欢
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2018-06-25
  • 2020-05-01
  • 2020-04-14
  • 2011-02-27
相关资源
最近更新 更多