错误使用的含义.dplyr 函数中的简写 [英] Meaning of error using . shorthand inside dplyr function

查看:20
本文介绍了错误使用的含义.dplyr 函数中的简写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个 dplyr::bind_rows 错误.这是一个非常微不足道的问题,因为我可以轻松绕过它,但我想了解错误消息的含义.

I'm getting a dplyr::bind_rows error. It's a very trivial problem, because I can easily get around it, but I'd like to understand the meaning of the error message.

我有新英格兰各州的一些人口组的以下数据,我想绑定这些相同值的副本,并将名称更改为新英格兰",以便我可以按名称分组并添加将它们列出来,为我提供各个州的价值,以及该地区的整体价值.

I have the following data of some population groups for New England states, and I'd like to bind on a copy of these same values with the name changed to "New England," so that I can group by name and add them up, giving me values for the individual states, plus an overall value for the region.

df <- structure(list(name = c("CT", "MA", "ME", "NH", "RI", "VT"), 
        estimate = c(501074, 1057316, 47369, 76630, 141206, 27464)),
        class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))

我这样做是作为更大的管道步骤流程的一部分,所以我不能只做 bind_rows(df, df %>% mutate(name = "New England")).dplyr 提供了方便的 . 将数据帧从一个函数传送到下一个函数的简写,但我不能使用它以某种方式将数据帧绑定到自身我愿意.

I'm doing this as part of a much larger flow of piped steps, so I can't just do bind_rows(df, df %>% mutate(name = "New England")). dplyr gives the convenient . shorthand for a data frame being piped from one function to the next, but I can't use that to bind the data frame to itself in a way I'd like.

做什么工作并获得我想要的输出:

What does work and gets me the output I want:

library(tidyverse)

df %>%
  # arbitrary piped operation
  mutate(name = str_to_lower(name)) %>%
  bind_rows(mutate(., name = "New England")) %>%
  group_by(name) %>%
  summarise(estimate = sum(estimate))
#> # A tibble: 7 x 2
#>   name        estimate
#>   <chr>          <dbl>
#> 1 ct            501074
#> 2 ma           1057316
#> 3 me             47369
#> 4 New England  1851059
#> 5 nh             76630
#> 6 ri            141206
#> 7 vt             27464

但是当我尝试用 . 速记做同样的事情时,我得到这个错误:

But when I try to do the same thing with the . shorthand, I get this error:

df %>%
  mutate(name = str_to_lower(name)) %>%
  bind_rows(. %>% mutate(name = "New England"))
#> Error in bind_rows_(x, .id): Argument 2 must be a data frame or a named atomic vector, not a fseq/function

就像我说的,用第一种方式做没问题,但我想理解错误,因为我写了很多多步管道代码.

Like I said, doing it the first way is fine, but I'd like to understand the error because I write a lot of multi-step piped code.

推荐答案

正如@aosmith 在评论中指出的那样,这是由于 magrittr 在这种情况下解析点的方式:

As @aosmith noted in the comments it's due to the way magrittr parses the dot in this case :

来自 ?'%>%':

使用点占位符作为 lhs

当点用作 lhs 时,结果将是一个功能序列,即一个适用的功能整个右侧链依次返回其输入.

When the dot is used as lhs, the result will be a functional sequence, i.e. a function which applies the entire chain of right-hand sides in turn to its input.

为了避免触发这种情况,对 lhs 上的表达式进行任何修改都可以:

To avoid triggering this, any modification of the expression on the lhs will do:

df %>%
  mutate(name = str_to_lower(name)) %>%
  bind_rows((.) %>% mutate(name = "New England"))

df %>%
  mutate(name = str_to_lower(name)) %>%
  bind_rows({.} %>% mutate(name = "New England"))

df %>%
  mutate(name = str_to_lower(name)) %>%
  bind_rows(identity(.) %>% mutate(name = "New England"))

这里有一个完全避免这个问题的建议:

Here's a suggestion that avoid the problem altogether:

df %>%
  # arbitrary piped operation
  mutate(name = str_to_lower(name)) %>%
  replicate(2,.,simplify = FALSE) %>%
  map_at(2,mutate_at,"name",~"New England") %>%
  bind_rows

# # A tibble: 12 x 2
#    name        estimate
#    <chr>          <dbl>
#  1 ct            501074
#  2 ma           1057316
#  3 me             47369
#  4 nh             76630
#  5 ri            141206
#  6 vt             27464
#  7 New England   501074
#  8 New England  1057316
#  9 New England    47369
# 10 New England    76630
# 11 New England   141206
# 12 New England    27464

这篇关于错误使用的含义.dplyr 函数中的简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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