dplyr :: recode为什么管道会产生错误? [英] dplyr::recode Why does pipe generate error?

查看:310
本文介绍了dplyr :: recode为什么管道会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在管道中使用recode,则会收到错误消息:

If I use recode in a pipe, I get an error:

df <-  df %>%
  recode(unit, .missing="g")

UseMethod("recode")中的错误:没有适用于'recode'的方法 应用于类"c('tbl_df','tbl','data.frame')"的对象

Error in UseMethod("recode") : no applicable method for 'recode' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"

如果我将其从管道中拉出,则效果很好:

If I pull it out of the pipe, it works fine:

df$unit <- recode(df$unit, .missing="g")

任何想法为何?如果可能的话,我想留在管道里.

Any ideas why? I'd like to stay in the pipe if possible.

推荐答案

dplyr中的baseR解决方案的等效项是在mutate中使用它:

An equivalent of the baseR solution in dplyr is to use it inside mutate:

df %>%
    mutate(unit = recode(unit, .missing="g"))

%>%之后直接链接recode会将数据帧作为第一个参数传递给recode,这与recode的参数不一致.第一个参数.x必须是向量;与其他dplyr函数不同,recode不会使用某些非标准的评估魔术来将unit解释为df中具有该名称的列.设计为直接与管道一起使用的大多数函数都有一个数据框作为其第一个参数和其输出.您可以在此处了解更多有关magrittr以及管道如何工作的信息.

Directly chaining recode after %>% will pass the data frame to recode as the first argument, which doesn't agree with recode's parameters. The first argument .x needs to be a vector; unlike some other dplyr functions recode doesn't use some non-standard evaluation magic to interpret unit as the column with that name in df. Most functions designed for direct use with the pipe have a data frame as their first argument and their output. You can read more about magrittr and how the pipe works here.

这篇关于dplyr :: recode为什么管道会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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