是否有解决方法将数据重塑回长格式

Is there a workaround to reshape data back to long format

我想将重塑从宽格式恢复为长格式,但我正在堆叠我应该如何进行,以便 a1_* 的值被放置在 a1 中。有解决方法吗?

library(tidyverse)

df <- tibble(
  id = c("s001", "s002", "s002", "s003", "s003",
              "s004", "s005"),
  a1 = c("A", "B", "C", "A", "D", "A", "B"),
  a2 = c(23, 24, 45, 23, 56, 45, 34),
  a3 = c("Primary", "Secondary", "Tertiary", "Primary",
         "Tertiary", "Secondary", "Primary"))

#---- Reshape long to wide

df <- df %>%
  group_by(id) %>%
  mutate(index = row_number()) %>% ungroup()


df_wide <- df %>%
  pivot_wider(id_cols = id,
              names_from = index,
              values_from = starts_with("a"))

#---- Reverse back

df_long <- df_wide %>%
  pivot_longer(!id,
    names_to = "index",
    names_prefix = "a\d_"

)


您需要做的是指定您使用分隔符左侧的特定值来标识唯一列(即 a1、...)和右侧 index.

我们可以为此使用 names_sep 参数,并使用 values_drop_na 删除原始 df.

中不存在的行
df_wide %>%
  pivot_longer(!id,
               names_to = c(".value", "index"),
               names_sep = "_",
               values_drop_na = T)
#> # A tibble: 7 × 5
#>   id    index a1       a2 a3       
#>   <chr> <chr> <chr> <dbl> <chr>    
#> 1 s001  1     A        23 Primary  
#> 2 s002  1     B        24 Secondary
#> 3 s002  2     C        45 Tertiary 
#> 4 s003  1     A        23 Primary  
#> 5 s003  2     D        56 Tertiary 
#> 6 s004  1     A        45 Secondary
#> 7 s005  1     B        34 Primary