Skip to content
Prev 315653 / 398503 Next

an apply question

On Jan 18, 2013, at 9:17 PM, m p wrote:

            
Unless this is a homework question then using `apply` seems inefficient.
You should not use the assignment operator in the expression that  
forms the consequent for `ifelse`. (Furthermore apply takes three  
arguments and you only have two.)

Vectorize:

mat1<-matrix(sample(-10:10,40,replace=TRUE),ncol=5)

  mat1[mat1 < 0] <- mat1[mat1 < 0]+24
  mat1
      [,1] [,2] [,3] [,4] [,5]
[1,]    2    4   23    1    0
[2,]   18    7   10    3   16
[3,]   10   16   16   16    0
[4,]    3    3    6   17    3
[5,]   21    0    6    9   16
[6,]   10    4    6    0    0
[7,]    7    8   21    0   20
[8,]   19    7   15   19    5
David Winsemius, MD
Alameda, CA, USA