2

I have a vector whose only entries are yes or no. I need to replace each yes with "Institutional" and every no with "Retail".

I have tried for loops, ifelse statements, case_when and replace functions, all to no avail. They all still return the same vector full of yes and no.

Frederick
  • 83
  • 3

2 Answers2

3

I would suggest this:

#Data
vec <- rep(c('yes','no'),10)
vec

[1] "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes" "no"  "yes"
[18] "no"  "yes" "no" 

#Replace
vec[vec=='yes'] <- 'Institutional'
vec[vec=='no'] <- 'Retail'
vec

[1] "Institutional" "Retail"        "Institutional" "Retail"        "Institutional" "Retail"       
[7] "Institutional" "Retail"        "Institutional" "Retail"        "Institutional" "Retail"       
[13] "Institutional" "Retail"        "Institutional" "Retail"        "Institutional" "Retail"       
[19] "Institutional" "Retail" 
Duck
  • 39,058
  • 13
  • 42
  • 84
1

Maybe you can try ifelse like below

v <- ifelse(v == "yes","Institutional","Retail")

or play some tricks with factor

v <- as.character(factor(v,levels=c("no","yes"), labels = c("Retail","Institutional")))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81