tidyr 文档中的 pivot_longer 问题 [英] pivot_longer issue from tidyr documentation

查看:41
本文介绍了tidyr 文档中的 pivot_longer 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注 Tidyr 官方文档,试图刷新因为我有一段时间没有使用它了.

涉及 billboard 数据集的示例之一产生以下错误:

库(tidyr)广告牌 %>%枢轴更长(cols = starts_with("wk"),names_to = "周",names_prefix = "wk",names_transform = list(week = as.integer),values_to =排名",values_drop_na = 真,)

pivot_longer(., cols = starts_with("wk"), names_to = "week", : 未使用的参数 (names_transform = list(week = as.integer)) 中的错误>

我对发生的事情有点困惑,因为正如我之前提到的,这在官方 tidyr 包文档中.

解决方案

tidyr -1.1.2 的当前 CRAN 版本返回输出没有任何错误

广告牌%>%枢轴更长(cols = starts_with("wk"),names_to = "周",names_prefix = "wk",names_transform = list(week = as.integer),values_to =排名",values_drop_na = 真,)# 小费:5,307 x 5艺术家曲目日期.进入周排名<chr><chr><日期><int><dbl>1 2 Pac Baby Don't Cry (Keep... 2000-02-26 1 872 2 Pac Baby Don't Cry (Keep... 2000-02-26 2 823 2 Pac Baby Don't Cry (Keep... 2000-02-26 3 724 2 Pac Baby Don't Cry (Keep... 2000-02-26 4 775 2 Pac Baby Don't Cry (Keep... 2000-02-26 5 876 2 Pac Baby Don't Cry (Keep... 2000-02-26 6 947 2 Pac Baby Don't Cry (Keep... 2000-02-26 7 998 2Ge+her 最难的部分... 2000-09-02 1 919 2Ge+her 最难的部分... 2000-09-02 2 8710 2Ge+her 最难的部分... 2000-09-02 3 92# ... 还有 5,297 行

I've been following along with the Tidyr official documentation, trying to freshen up on it because I haven't used it in awhile.

One of the examples involving the billboard dataset produces the following error:

library(tidyr)

billboard %>% 
  pivot_longer(
    cols = starts_with("wk"), 
    names_to = "week", 
    names_prefix = "wk",
    names_transform = list(week = as.integer),
    values_to = "rank",
    values_drop_na = TRUE,
  )

Error in pivot_longer(., cols = starts_with("wk"), names_to = "week", : unused argument (names_transform = list(week = as.integer))

I'm a bit confused as to what's going on because as I mentioned earlier, this is in the official tidyr package documentation.

解决方案

The current CRAN version for tidyr -1.1.2 returns the output without any errors

billboard %>% 
  pivot_longer(
     cols = starts_with("wk"), 
     names_to = "week", 
     names_prefix = "wk",
     names_transform = list(week = as.integer),
     values_to = "rank",
     values_drop_na = TRUE,
   )
# A tibble: 5,307 x 5
   artist  track                   date.entered  week  rank
   <chr>   <chr>                   <date>       <int> <dbl>
 1 2 Pac   Baby Don't Cry (Keep... 2000-02-26       1    87
 2 2 Pac   Baby Don't Cry (Keep... 2000-02-26       2    82
 3 2 Pac   Baby Don't Cry (Keep... 2000-02-26       3    72
 4 2 Pac   Baby Don't Cry (Keep... 2000-02-26       4    77
 5 2 Pac   Baby Don't Cry (Keep... 2000-02-26       5    87
 6 2 Pac   Baby Don't Cry (Keep... 2000-02-26       6    94
 7 2 Pac   Baby Don't Cry (Keep... 2000-02-26       7    99
 8 2Ge+her The Hardest Part Of ... 2000-09-02       1    91
 9 2Ge+her The Hardest Part Of ... 2000-09-02       2    87
10 2Ge+her The Hardest Part Of ... 2000-09-02       3    92
# … with 5,297 more rows

这篇关于tidyr 文档中的 pivot_longer 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆