如何在具有多个列对的数据框中按名称连接两对列并移动行

Tar*_*Jae 1 r dplyr tidyr

我有这个数据框:

     id    a1    a2    b1    b2    c1    c2
  <int> <int> <int> <int> <int> <int> <int>
1     1    83    33    55    33    85    86
2     2    37     0    60    98    51     0
3     3    97    71    85     8    44    40
4     4    51     6    43    15    55    57
5     5    28    53    62    73    70     9
Run Code Online (Sandbox Code Playgroud)
df <- structure(list(id = 1:5, a1 = c(83L, 37L, 97L, 51L, 28L), a2 = c(33L, 
0L, 71L, 6L, 53L), b1 = c(55L, 60L, 85L, 43L, 62L), b2 = c(33L, 
98L, 8L, 15L, 73L), c1 = c(85L, 51L, 44L, 55L, 70L), c2 = c(86L, 
0L, 40L, 57L, 9L)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)

我想: 通过将第二列的每一行向下移动 1 并用两列的字符命名新列,将具有相同起始字符的列合并为一列。

我想要的输出:

      id     a     b     c
   <dbl> <dbl> <dbl> <dbl>
 1     1    83    55    85
 2     1    33    33    86
 3     2    37    60    51
 4     2     0    98     0
 5     3    97    85    44
 6     3    71     8    40
 7     4    51    43    55
 8     4     6    15    57
 9     5    28    62    70
10     5    53    73     9
Run Code Online (Sandbox Code Playgroud)

我尝试过使用lag函数,但我不知道如何同时组合和移动列!

澄清一张图: 在此处输入图片说明

Ano*_*n R 5

您可以使用以下解决方案。我还修改了您的数据集并添加了一id列:

library(tidyr)

df %>%
  pivot_longer(!id, names_to = c(".value", NA), names_pattern = "([[:alpha:]])(\\d)")

# A tibble: 10 x 4
      id     a     b     c
   <int> <int> <int> <int>
 1     1    83    55    85
 2     1    33    33    86
 3     2    37    60    51
 4     2     0    98     0
 5     3    97    85    44
 6     3    71     8    40
 7     4    51    43    55
 8     4     6    15    57
 9     5    28    62    70
10     5    53    73     9
Run Code Online (Sandbox Code Playgroud)