Pivot_longer/pivot_wider 超过一列

Pivot_longer/pivot_wider on more than one column

我有以下数据集

代码如下:

df = structure(list(UID = structure(1:5, .Label = c("C001", "C002", 
"C003", "C004", "C005"), class = "factor"), Shop_Code = structure(1:5, .Label = c("A", 
"B", "C", "D", "E"), class = "factor"), Shop_District_Code = structure(1:5, .Label = c("AA", 
"BB", "CC", "DD", "EE"), class = "factor"), Bread = c(5, 4.8, 
4.5, 4.2, 5), Apple = c(3, 2.8, 3.1, 3.4, 3.2), Coke = c(3, 2.6, 
2.8, 3, 3.1), Bread_1 = c(NA, NA, NA, 4.6, NA), Apple_1 = c(NA, 
NA, NA, 2.6, NA), Coke_1 = c(NA, NA, NA, 2.2, NA), Bread_2 = c(NA, 
NA, 4.6, NA, 5.1), Apple_2 = c(NA, NA, 3, NA, 3.3), Coke_2 = c(NA, 
NA, 3, NA, 3.2), Bread_3 = c(NA, NA, 4.8, NA, NA), Apple_3 = c(NA, 
NA, 2.7, NA, NA), Coke_3 = c(NA, NA, 2.6, NA, NA)), class = "data.frame", row.names = c(NA, 
-5L))

有人知道如何使用 pivot_wider/pivot_longer(或任何其他函数)将扩展列作为行附加到底部,如下面的 table 吗?

pivot_longer(df, -(1:3), 
        names_to = '.value', 
        names_pattern = '([^_]+)', 
        values_drop_na = TRUE)

# A tibble: 9 x 6
  UID   Shop_Code Shop_District_Code Bread Apple  Coke
  <fct> <fct>     <fct>              <dbl> <dbl> <dbl>
1 C001  A         AA                   5     3     3  
2 C002  B         BB                   4.8   2.8   2.6
3 C003  C         CC                   4.5   3.1   2.8
4 C003  C         CC                   4.6   3     3  
5 C003  C         CC                   4.8   2.7   2.6
6 C004  D         DD                   4.2   3.4   3  
7 C004  D         DD                   4.6   2.6   2.2
8 C005  E         EE                   5     3.2   3.1
9 C005  E         EE                   5.1   3.3   3.2