如何将具有熔化或聚集功能的 2 列中的 6 个特定列堆叠?

How to stack 6 specific columns in 2 columns with melt or gather function?

这是我的问题的一个例子:

我想从

A   B   C   D   E   F   G   H
x1  x2  x3  x4  x5  x6  x7  x8
y1  y2  y3  y4  y5  y6  y7  y8
z1  z2  z3  z4  z5  z6  z7  z8

A   B   CDE FGH  
x1  x2  x3  x6  
x1  x2  x4  x7  
x1  x2  x5  x8  
y1  y2  y3  y7  
y1  y2  y4  y6  
y1  y2  y5  y8  

我只能使用此代码将 3 列堆叠为一列

NewData= melt(setDT(Data),measure = list(c(6,7,8)), value.name = "FGH ")

我们可以使用patterns

library(data.table)
melt(setDT(Data), measure = patterns("^[CDE]", "^[FGH]"), 
        value.name = c("CDE", "FGH"))[, variable := NULL][]

unite

的另一个选项
library(dplyr)
library(tidyr)
Data %>% 
    unite(CDE, C, D, E) %>%
    unite(FGH, F, G, H) %>% 
    separate_rows(CDE, FGH)

数据

Data <- structure(list(A = c("x1", "y1", "z1"), B = c("x2", "y2", "z2"
), C = c("x3", "y3", "z3"), D = c("x4", "y4", "z4"), E = c("x5", 
"y5", "z5"), F = c("x6", "y6", "z6"), G = c("x7", "y7", "z7"), 
    H = c("x8", "y8", "z8")), class = "data.frame", row.names = c(NA, 
-3L))

我们可以获取长格式的数据,为 c('C', 'D', 'E') 和其他值创建单独的值,然后获取宽格式的数据。

library(dplyr)
library(tidyr)

df %>%
  pivot_longer(cols = -(A:B)) %>%
  mutate(name = ifelse(name %in% c('C', 'D', 'E'), 'CDE', 'FGH')) %>%
  group_by(name) %>%
  mutate(row = row_number()) %>%
  pivot_wider() %>%
  select(-row)


# A tibble: 9 x 4
#  A     B     CDE   FGH  
#  <chr> <chr> <chr> <chr>
#1 x1    x2    x3    x6   
#2 x1    x2    x4    x7   
#3 x1    x2    x5    x8   
#4 y1    y2    y3    y6   
#5 y1    y2    y4    y7   
#6 y1    y2    y5    y8   
#7 z1    z2    z3    z6   
#8 z1    z2    z4    z7   
#9 z1    z2    z5    z8   

数据

df <- structure(list(A = c("x1", "y1", "z1"), B = c("x2", "y2", "z2"
), C = c("x3", "y3", "z3"), D = c("x4", "y4", "z4"), E = c("x5", 
"y5", "z5"), F = c("x6", "y6", "z6"), G = c("x7", "y7", "z7"), 
H = c("x8", "y8", "z8")), class = "data.frame", row.names = c(NA, -3L))