If I use recode in a pipe, I get an error:
如果我在管道中使用重新编码,我收到一个错误:
df <- df %>%
recode(unit, .missing="g")
Error in UseMethod("recode") : no applicable method for 'recode' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')"
UseMethod(“recode”)中的错误:没有适用于“recode”的方法应用于类“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.
有什么想法吗?如果可能的话,我想留在管道里。
1 个解决方案
#1
13
An equivalent of the baseR solution in dplyr
is to use it inside mutate
:
在dplyr中等效的baseR解决方案是在mutate中使用它:
df %>%
mutate(unit = recode(unit, .missing="g"))
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.
在%>%之后直接链接重新编码将传递数据帧作为第一个参数重新编码,这与recode的参数不一致。第一个参数.x需要是一个向量;与其他一些dplyr函数不同,recode不使用某些非标准的评估魔法将单位解释为df中具有该名称的列。设计用于直接与管道一起使用的大多数功能都有一个数据框作为其第一个参数及其输出。您可以在此处阅读有关magrittr以及管道如何工作的更多信息。
#1
13
An equivalent of the baseR solution in dplyr
is to use it inside mutate
:
在dplyr中等效的baseR解决方案是在mutate中使用它:
df %>%
mutate(unit = recode(unit, .missing="g"))
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.
在%>%之后直接链接重新编码将传递数据帧作为第一个参数重新编码,这与recode的参数不一致。第一个参数.x需要是一个向量;与其他一些dplyr函数不同,recode不使用某些非标准的评估魔法将单位解释为df中具有该名称的列。设计用于直接与管道一起使用的大多数功能都有一个数据框作为其第一个参数及其输出。您可以在此处阅读有关magrittr以及管道如何工作的更多信息。