I am well acquainted with the R standard ifelse statement and how to create nested ifelse statements. However, I want to create a “better” version, so I don’t need to copy / paste ifelse many times.
Take this nested ifelse statement, for example:
df <- data.frame(b = 1:5)
df$a <- ifelse(df$b == 1,1,
ifelse(df$b == 2,2,
ifelse(df$b == 3,3,4)))
Instead, I would like to create such a function as I could in this way:
df$a <- myFunction(df$b == 1,1,
df$b == 2,2,
df$b == 3,3,4)
I would like the function to be able to select how many arguments I entered, and therefore I know how many ifelse statements to include, and then connect the arguments to the correct position, right down to how much I want.
There is still some repetition, but when creating longer nested ifelse statements, it would be nice not to repeat this piece of code, and then try to keep track of paren's ending.