If I use recode in a pipe, I get an error:
df <- df %>%
recode(unit, .missing=\"g\")
Error in UseMethod(\"recode\")
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.