Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Evaluate an operation in a dataframe's context

$
0
0

I'm working with formulas in a package, and I'd like to remove all non-numeric terms.

My problem is that, since the base formula may contain operations, I cannot evaluate some terms directly.

Here is a reprex:

datax=mtcars
op = c("mpg", "log(mpg)", "ifelse(mpg>2, '>2', '<=2')")
sapply(op, function(x){ is.numeric(datax[[x]]) })
#T,F,F
sapply(op, function(x){ is.numeric(with(data, get(x))) })
#Error in get(x) : object 'log(mpg)' not found

#Expected output:
#T,T,F

I know I could use attach(datax) but this function is highly not recommended and I'd rather avoid it.

Is there a way to evaluate the operation in the context of the dataframe so I can test if the result is numeric or not?

NB: I'm not sure my title should be about evaluation or formula terms dropping, feel free to propose an edit.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>