using Reduce/do.call with ifelse

Viewed 504

This is purely a curiosity (learning more about Reduce). There are way better methods to achieve what I'm doing and I am not interested in them.

Some people use a series of nested ifelse commands to recode/look up something. Maybe it looks like this:

set.seed(10); x <- sample(letters[1:10], 300, T)
ifelse(x=="a", 1, 
    ifelse(x=="b", 2,
    ifelse(x=="c", 3, 
    ifelse(x=="d", 4, 5))))

Is there a way to use either do.call or Reduce with the ifelse to get the job done a little more eloquently?

2 Answers
Related