Home > Blockchain >  Evaluate condition stored in character vector
Evaluate condition stored in character vector

Time:11-21

How can I evaluate an ifelse() statement using a condition stored in a character vector?

For example:

a <- 1
b <- 2
condition <- ">"

ifelse(a condition b, print("good"), print("bad))

CodePudding user response:

You could try this:

ifelse(eval(parse(text = paste0(a, condition, b))), 'good', 'bad')

CodePudding user response:

Also would work:

ifelse(do.call(condition, list(a, b)), "good", "bad")

As > is a function in R.

  • Related