Skip to content
Prev 296083 / 398506 Next

Separate Array Variable Content

On May 30, 2012, at 1:04 PM, ilai wrote:

            
Here's another approach. It involves creating an expression vector and  
then evaluating on a named list argument:

 > exprvec <- expression(ABC > 50, PQR < 50, ABC < 30 & XYZ < 40)
 > (MyVals2 <- list(ABC =c(10, 20, 30), PQR =c(40, 50, 60), XYZ=c( 70,  
80, 90)) )
$ABC
[1] 10 20 30

$PQR
[1] 40 50 60

$XYZ
[1] 70 80 90

 > sapply(exprvec, function(ep) with(MyVals2, eval(ep )) )
       [,1]  [,2]  [,3]
[1,] FALSE  TRUE FALSE
[2,] FALSE FALSE FALSE
[3,] FALSE FALSE FALSE

There can be problems with using `with` inside functions and I'm not  
smart enough to know why I'm able to get away with it here.