[R] Separate Array Variable Content

David Winsemius dwinsemius at comcast.net
Wed May 30 23:35:36 CEST 2012


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

> If you haven't done so you *must* read an Introduction to R. The only
> reason this is a problem is Myarray is a character string, not a  
> function
> or expression to be evaluated. I think this will get you what you want
> though:
>
> # In the future use the output of ?dput to provide data to this list
> (MyMatrix <- structure(c(10, 20, 30, 40, 50, 60, 70, 80, 90), .Dim =  
> c(3L,
> 3L), .Dimnames = list(NULL, c("ABC", "PQR", "XYZ"))))
>
> # DO NOT use rich font !!! in plain text it adds '*' to the bold names
> which is more than annoying...
> MyArray <-  c("ABC>50","PQR<50","ABC<30 & XYZ<40")
>
> # finally the answer:
> sapply(MyArray,function(x)  
> eval(parse(text=x),as.data.frame(MyMatrix)))

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.

-- 
David
>
> HTH
>
> On Wed, May 30, 2012 at 12:44 AM, Rantony <antony.akkara at ge.com>  
> wrote:
>
>> Hi,
>>
>> I am new in R,
>>
>> i have a matrix like this
>>
>> MyMatrix <-
>> *ABC   PQR    XYZ*
>> 10       20        30
>> 40       50        60
>> 70       80        90
>>
>> And, i have an array containing some conditions like this,
>> MyArray <-  c("*ABC*>50","*PQR*<50","*ABC*<30 &* XYZ*<40")
>>
>> "ABC>50"
>> "PQR<50"
>> "ABC<30 & XYZ<40"
>>
>> My purpose what is, i need to check this conditions in *MyArray* with
>> *MyMatrix* value for particular column
>>
>> How it is possible ?
>>
>> - Thanks
>> Antony.
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Separate-Array-Variable-Content-tp4631800.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list