dplyr::recode Why does pipe generate error?

后端 未结 1 1572
醉酒成梦
醉酒成梦 2021-01-01 22:36

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

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

Error in UseMethod(\"recode\")

1条回答
  •  别那么骄傲
    2021-01-01 23:20

    An equivalent of the baseR solution in dplyr is to use it inside 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.

    0 讨论(0)
提交回复
热议问题