[R] matrix - change values

Greg Snow Greg.Snow at intermountainmail.org
Thu Dec 14 17:25:56 CET 2006


A matrix is already a vector, you don't need to do the transformations,
just do the same thing directly:

> tmp <- matrix( sample(1:12), ncol=3 )
> tmp
     [,1] [,2] [,3]
[1,]   11    1    6
[2,]    3    7    9
[3,]    4   12    8
[4,]    2    5   10
> tmp[tmp > 5] <- 0
> tmp
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    3    0    0
[3,]    4    0    0
[4,]    2    5    0

If on the other hand, your matrix is really a data frame then functions
like lapply, sapply, transform may help.

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
 

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
robert-mcfadden at o2.pl
Sent: Thursday, December 14, 2006 7:01 AM
To: r-help at stat.math.ethz.ch
Subject: [R] matrix - change values

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