【问题标题】:Why do I get an error when trying to use names_glue in tidyr::pivot_wider?为什么在 tidyr::pivot_wider 中尝试使用 names_glue 时会出错?
【发布时间】:2020-06-02 02:58:56
【问题描述】:

我在尝试使用 tidyr::pivot_wider 中的 names_glue 参数时收到未使用的参数错误。这是帮助页面中的示例和我的错误:

library(tidyr)
us_rent_income %>%
  pivot_wider(
    names_from = variable,
    names_glue = "{variable}_{.value}",
    values_from = c(estimate, moe)
  )
Error in pivot_wider(., names_from = variable, names_glue = "{variable}_{.value}",  :
  unused argument (names_glue = "{variable}_{.value}")

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
...
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tidyr_1.0.2

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0 compiler_3.6.1   magrittr_1.5     assertthat_0.2.1
 [5] R6_2.4.1         pillar_1.4.3     glue_1.4.1       dplyr_0.8.5
 [9] tibble_2.1.3     crayon_1.3.4     Rcpp_1.0.4       vctrs_0.2.4
[13] lifecycle_0.2.0  pkgconfig_2.0.3  rlang_0.4.5      purrr_0.3.3

我更新了tidyr 和glue。

我应该注意,没有 names_glue 参数,工作正常:

us_rent_income %>%
  pivot_wider(
    names_from = variable,
    names_sep = ".",
    values_from = c(estimate, moe)
  )

【问题讨论】:

    标签: r tidyr


    【解决方案1】:

    根据变更日志,pivot_wider() 似乎在 1.1.0 版本中获得了 names_glue 参数。你显示的是 1.0.2,对吧? Package ‘tidyr’ version 1.1.0 -also- Hadley's tidyr 1.1.0 blog announcement

    这应该可以解决您的问题:

    install.packages("tidyr")
    library(tidyr)
    us_rent_income %>%
      pivot_wider(
        names_from = variable,
        names_glue = "{variable}_{.value}",
        values_from = c(estimate, moe)
      )
    

    请注意,您可能还必须将 vctrs 包更新到版本 0.3.0。

    【讨论】:

    • 谢谢。我的错误是使用update.packages() 而不是安装最新版本。为了将来参考,install.packages("tidyr") 还安装了最新版本的vctrs。
    猜你喜欢
    • 2021-01-14
    • 1970-01-01
    • 2018-12-12
    • 2021-10-08
    • 2013-03-07
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多