将变量传递给 tidyr::pivot_wider 的 names_glue 参数

Passing variables into the names_glue parameter of tidyr::pivot_wider

这里有一些愚蠢的数据,我们使用两个名称将其扩展得更广:

library(tidyr)

df <- data.frame(
    food = c('banana','banana','banana','banana','cheese','cheese','cheese','cheese'),
    binary = c(rep(c('yes','no'), 4)),
    car = c('toyota','subaru','mazda','skoda','toyota','subaru','mazda','skoda'),
    fun = c(2,4,3,6,2,4,2,3))

df %>%
    pivot_wider(
        id_cols = food,
        names_from = c(car, binary),
        values_from = fun)

如果我们想改变新变量名的格式,例如,从 toyota_yesyes_toyota,我们使用 names_glue 参数:

df %>%
    pivot_wider(
        id_cols = food,
        names_from = c(car, binary),
        names_glue = "{binary}_{car}",
        values_from = fun)

我面临的问题是找到正确的语法来将变量名传递给 names_glue 参数。将变量传递给 names_from 很容易,例如:

var1 <- 'car'
var2 <- 'binary'
df %>%
    pivot_wider(
        id_cols = food,
        names_from = c(var1, var2),
        values_from = fun)

但是我们不能直接用 names_glue:

df %>%
    pivot_wider(
        id_cols = food,
        names_from = c(var1, var2),
        names_glue = "{var1}_{var2}",
        values_from = fun)

Error: Column names car_binary, car_binary, and car_binary must not be duplicated.

大概它正在评估变量并将结果字符串(即 'car' 或 'binary')传递给胶水函数。我玩过一些通常用于整洁评估的东西(!!sym(...) 等),但没有任何效果。期望的输出如下,使用 names_glue 参数的变量:

# A tibble: 2 x 5
  food   yes_toyota no_subaru yes_mazda no_skoda
  <fct>       <dbl>     <dbl>     <dbl>    <dbl>
1 banana          2         4         3        6
2 cheese          2         4         2        3

Here is the commit that added the names_glue parameter to pivot_wider -- although I can't really figure out how to troubleshoot with this.

您可以使用sprtinf/paste0构造字符串:

library(tidyr)
df %>%
  pivot_wider(
    id_cols = food,
    names_from = c(var1, var2),
    names_glue = sprintf('{%s}_{%s}', var2, var1),
    values_from = fun)

#  food   yes_toyota no_subaru yes_mazda no_skoda
#  <chr>       <dbl>     <dbl>     <dbl>    <dbl>
#1 banana          2         4         3        6
#2 cheese          2         4         2        3

我们可以用 glue

创建模式
library(dplyr)
library(tidyr)
df %>%
     pivot_wider(
         id_cols = food,
         names_from = c(var1, var2),
         names_glue = glue::glue("{[var1]}_{[var2]}", .open = '[', .close = ']'),
         values_from = fun)
# A tibble: 2 x 5
#  food   toyota_yes subaru_no mazda_yes skoda_no
#  <chr>       <dbl>     <dbl>     <dbl>    <dbl>
#1 banana          2         4         3        6
#2 cheese          2         4         2        3