[R] Help wit matrices

Roland Rau roland.rproject at gmail.com
Fri Aug 10 20:53:26 CEST 2007


I hope you don't mind that I offer also two solutions. No.1 is really 
bad. No.2 should be on par with the other ones.

Best,
Roland



mydata <- matrix(rnorm(10*10), ncol=10)

threshold.value <- 1.5

mydata2 <- matrix(0, nrow=nrow(mydata), ncol=ncol(mydata))
mydata3 <- matrix(0, nrow=nrow(mydata), ncol=ncol(mydata))


### not really the way to go:
for (i in 1:nrow(mydata)) {
   for (j in 1:ncol(mydata)) {
     if (mydata[i,j]>threshold.value) {
       mydata2[i,j] <- 1
     }
   }
}
### a better way...
mydata3[mydata > threshold.value] <- 1
mydata2
mydata3



Lanre Okusanya wrote:
> Hello all,
> 
> I am working with a 1000x1000 matrix, and I would like to return a
> 1000x1000 matrix that tells me which value in the matrix is greater
> than a theshold value (1 or 0 indicator).
> i have tried
>   mat2<-as.matrix(as.numeric(mat1>0.25))
> but that returns a 1:100000 matrix.
> I have also tried for loops, but they are grossly inefficient.
> 
> THanks for all your help in advance.
> 
> Lanre
> 
> ______________________________________________
> 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