[R] matrix - change values

apjaworski at mmm.com apjaworski at mmm.com
Thu Dec 14 17:17:47 CET 2006


Rob,

Try

a[a>5]<-0

Yup.  It works for matrices (and for arrays).  It also works with the
replacement value being a vector.  For example, try

b <- array(1:24, dim=c(3, 4, 2))
b[(b>8) & (b<17)] <- 101:108

I think the reason it works like this is that internally array are stored
as vectors.

Cheers,

Andy

__________________________________
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122


                                                                           
             robert-mcfadden at o                                             
             2.pl                                                          
             Sent by:                                                   To 
             r-help-bounces at st         r-help at stat.math.ethz.ch            
             at.math.ethz.ch                                            cc 
                                                                           
                                                                   Subject 
             12/14/2006 08:01          [R] matrix - change values          
             AM                                                            
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Dear R Users,
I have a matrix A, and I want to change every value of this matrix if these
values are greater than an assuming value. For a vector it is simple, e.g.
a<-c(1:10); a[a>5]<-0.
Of course, I can change matrix to vector, assign a value then change vector
to matrix. But does there exist simpler way?
Any suggestion are appreciate.
Rob

______________________________________________
R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list