Since you can convert a string to an executable expression, for example:
eval(parse(text = "3 > 1"))
Or, a line vector of executable expressions, using for example:
sapply(parse(text = paste0(3:4, "<4")), eval)
You can use this in your case to pass a free text condition using:
getIndexesWhen<-function(g, colname, condition) { a <- as.vector(g[,colname]) which(sapply(parse(text = paste0(a, condition)), eval)) }
This evaluates the column vector relative to the provided condition.
g <- data.frame(C1 = c(2, 3, 4)) getIndexesWhen(g, "C1", ">=3")
source share