0

Here's my data :

dataset <- c("h", "H", "homme", "masculin", "f", "femme", "épouse")

How can I replace text values of the vector like :

  • "femme" -> "f"
  • "épouse" ->"f"
  • "Homme"-> "h"
  • "masculin" -> "h"

What I tried for "femme" -> "f"

test_out <- sapply(dataset, switch,
         "f"="femme")
test_out 

Expected result :

  "h"        "h"        "h"    "masculin" "f"        "f"    "f"  
Wilcar
  • 2,349
  • 2
  • 21
  • 48

1 Answers1

2

Try gsub with regular expressions:

 dataset = gsub("^((?!h).*)$", "f", gsub("^((h|H|m).*)$", "h", dataset), perl=TRUE)
juan
  • 398
  • 1
  • 14