We should always provide a minimal reproducible example:
df <- data.frame(x=c(1,1,2,2,3,3))
You didn't specifiy the package for recode
so I assumed dplyr
. ?dplyr::recode
tells us how the arguments should be passed to the function. In the original question "c(1=2; 2=1; 3=3"
is a string (i.e. not an R expression but a character string "c(1=2; 2=1; 3=3"). To make it an R expression we have to get rid of the double quotes and replace the ;
with ,
. Additionally, we need a closing bracket i.e. c(1=2, 2=1, 3=3)
. But still, as ?dplyr::recode
tells us, this is not the way to pass this information to recode
:
Solution using dplyr::recode
:
dplyr::recode(df$x, "1"=2, "2"=1, "3"=3)
Returns:
[1] 2 2 1 1 3 3