将具有多组列的长格式数据框转换为宽格式

Transform long format dataframe with multiples sets of columns into wide format

如何将具有多组列的长格式数据框转换为宽格式?

df <- data.frame(
  id = c(1, 2),
  uid = c("m1", "m2"),
  germ = c(23, 24),
  category = c("x1", "x2"),
  mineral = c(78, 10))

名称来自 uidcategory,而值来自 germmineral。我已经尝试使用下面的代码来解决这个问题,但名称不是我想要的

out_df <- df %>% 
  tidyr::pivot_wider(names_from = c(uid, category), values_from = c(germ, mineral))

这是我希望输出的样子

out_df <- data.frame(id = c(1, 2),
                     m1 = c(23, NA),
                     m2 = c(NA, 24),
                     x1 = c(78, NA),
                     x2 = c(NA, 10))

获得预期输出的一种简单方法是应用 pivot_wider 两次:

df %>% 
  pivot_wider(names_from = "uid", values_from = "germ") %>% 
  pivot_wider(names_from = "category", values_from = "mineral")

# A tibble: 2 x 5
#     id    m1    m2    x1    x2
#  <dbl> <dbl> <dbl> <dbl> <dbl>
#1     1    23    NA    78    NA
#2     2    NA    24    NA    10