Search code examples
rfunctionsubseteval

Finding matches from an object of class "call" in R


Given the function call object a, I was wondering how I could find instances of n for which control is T and F, also how to know what is the value of long for each of such cases?

For example, in the below case, I expect to subset 31 32 35 as ns for which control is T and 30 33 34 as ns for which control is F.

The same goes with long. In other words, I'm looking for a subsetting strategy from a call object?

foo <- function(n , long, control, ...){

 match.call()
}

# Example:
a <- foo(n = 30:35, long = c(1, 2, 2, 3, 1,1), control = c(F, T, T, F, F, T))

Solution

  • If we need to get the value from 'a'

    lst1 <- as.list(a)
    eval(lst1$n)[eval(lst1$control)]
    #[1] 31 32 35
    eval(lst1$n)[eval(lst1$control) & eval(lst1$long)==1]
    #[1] 35