1

I have

myColors <- c("red", "purple", "blue", "blue", "orange", "red", "orange")
library(dplyr)
recode(myColors, red="rot", blue="blau", purple="violett")

However if my data have spaces in them this method does not work

myColors <- c("Color red", "Color purple", "Color blue", "Color blue", "Color orange", "Color red", "Color orange")
recode(myColors, Color red="rot", Color blue="blau", Color purple="violett")

Is there anything I can do to fix this other than changing the data?

manro
  • 3,529
  • 2
  • 9
  • 22
ECII
  • 10,297
  • 18
  • 80
  • 121

1 Answers1

2

If your categories have a space or ... you have to wrap them in quotes or backticks:

myColors <- c("Color red", "Color purple", "Color blue", "Color blue", "Color orange", "Color red", "Color orange")

dplyr::recode(myColors, "Color red" = "rot", `Color blue` = "blau", "Color purple" = "violett")
#> [1] "rot"          "violett"      "blau"         "blau"         "Color orange"
#> [6] "rot"          "Color orange"
stefan
  • 90,330
  • 6
  • 25
  • 51