Dplyr’s mutate function takes the data frame as the first term, and then the variable you want to create or change with its formula.
So to add the variable variable.r to data, you’d write mutate(data, variable.r = your_formula_here) or alternatively, data %>% mutate(variable.r = your_formula_here).
Function recode from dplyr takes separate named arguments to describe replacements (see ... description). In your case it is sufficient to change the code into:
As your question has been answered, would you mind choosing a solution properly ? I saw you put it in the title, but you can mark it using discourse feature.
Here’s how to do it:
recode should be good for your situation! But usually it's used inside mutate (I've never tried using it with a regular assignment). Also, the arguments in recode (as in any function) should be separated by commas, not semi-colons. What kind of error do you get if you try:
recode takes a vector and gives back a vector. You can use it that way without mutate. It works well with mutate for the same reason. Both are working, as in the example below
library(dplyr, warn.conflicts = FALSE)
data <- tibble(variable = c("strongly disagree","agree", "disagree", "strongly agree"))
data <- data %>%
mutate(variable.r = recode(variable,
"strongly disagree" = 0, "disagree" = 1, "agree" = 2, "strongly agree" = 3))
#> Warning: le package 'bindrcpp' a été compilé avec la version R 3.4.4
data$variable.r2 <- recode(data$variable,
"strongly disagree" = 0, "disagree" = 1, "agree" = 2, "strongly agree" = 3)
data
#> # A tibble: 4 x 3
#> variable variable.r variable.r2
#> <chr> <dbl> <dbl>
#> 1 strongly disagree 0 0
#> 2 agree 2 2
#> 3 disagree 1 1
#> 4 strongly agree 3 3