[R] Convert values below threshold to 0 (different threshold/column)

arun smartpink111 at yahoo.com
Mon Feb 17 17:54:10 CET 2014


Hi,
May be this helps:
set.seed(49)
mat1 <- matrix(sample(120,40*13,replace=TRUE),ncol=13)

#threshold values
set.seed(38)
th1 <- sample(30,13,replace=TRUE)
indx <- mapply("<",as.data.frame(mat1),th1)
#or
indx1 <- !mapply(findInterval,as.data.frame(mat1),th1)
#or
indx2 <- t((t(mat1)-th1) <0)
identical(indx,indx1)
#[1] TRUE

identical(which(indx),which(indx2))
#[1] TRUE


mat1[indx1] <- NA #or 0
mat1

A.K.


Hi, 
I am a beginner and have a 872x13 double matrix. For my calculations
 I need to replace all values below specific thresholds with 0 or NA. My
 problem is that I have for each column a different threshold. 
Thus far, I tried to substract the threshold and then replace a 
values below 0 with NA and calculated the means using colMeans(M, na.rm =
 TRUE). However, for my purpose I don't want to substract. I am sure 
there is an easy function which can just define a specific threshold for
 each column? 
I already generated a threshold matrix. 
Thanks for your help. 
R



More information about the R-help mailing list