[R] change cell values

arun smartpink111 at yahoo.com
Wed Jul 3 23:48:14 CEST 2013


Hi,

set.seed(24) 
mat1=matrix(rnorm(12),3)
set.seed(28)
mat2=matrix(rnorm(12),3)
 indx<- mat1<1 & mat2<1
mat1[indx]<-NA
 mat2[indx]<-NA
 mat1
#     [,1] [,2] [,3]        [,4]
#[1,]   NA   NA   NA 0.002311942
#[2,]   NA   NA   NA          NA
#[3,]   NA   NA   NA 0.598269113
 mat2
#     [,1] [,2] [,3]     [,4]
#[1,]   NA   NA   NA 1.841481
#[2,]   NA   NA   NA       NA
#[3,]   NA   NA   NA 1.520367
A.K.

----- Original Message -----
From: JiangZhengyu <zhyjiang2006 at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, July 3, 2013 5:27 PM
Subject: [R] change cell values




Dear R experts,

I have two  matrices (mat1 & mat2) with the same dimension & the cells (row and column) are corresponding to each other.

I want to change cell values to NA given values of the corresponding cells in mat1 and mat2 are both <1.

E.g. both mat1[2,3] and mat2[2,3] are <1, I will put mat1[2,3]=NA, and mat2[2,3]=NA; if either mat1[2,3]>=1 or  mat2[2,3]>=1, I will save both cells.

I tried the code, but not working. Could anyone can help fix the problem?

mat1[mat1<1&mat2<1]=NA
mat2[mat1<1&mat2<1]=NA


> mat1=matrix(rnorm(12),3)
> mat2=matrix(rnorm(12),3)
> mat1
           [,1]       [,2]       [,3]       [,4]
[1,] -1.3387075 -0.7142333 -0.5614211  0.1846955
[2,] -0.7936087 -0.2215797 -0.3686067  0.7328731
[3,]  0.6505082  0.1826019  1.5577883 -1.5580384
> mat2
           [,1]       [,2]       [,3]       [,4]
[1,]  0.4331573 -1.8086826 -1.7688123 -1.4278934
[2,] -0.1841451  0.1738648 -1.1086942  1.3065109
[3,] -1.0827245 -0.4143808 -0.6889405  0.4046203

                          
    [[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.




More information about the R-help mailing list