Re: [R] efficient way to replace a range of numeric with a integer in a matrix

2014-08-11 Thread David Winsemius
On Aug 11, 2014, at 3:27 PM, Jinsong Zhao wrote: > On 2014/8/11 14:50, William Dunlap wrote: >> You can use >> m[m > 0 & m <= 1.0] <- 1 >> m[m > 1 ] <- 2 >> or, if you have lots of intervals, something based on findInterval(). E.g., >> m[] <- findInterval(m, c(-Inf, 0, 1, Inf)) - 1 >

Re: [R] efficient way to replace a range of numeric with a integer in a matrix

2014-08-11 Thread Jinsong Zhao
On 2014/8/11 14:50, William Dunlap wrote: You can use m[m > 0 & m <= 1.0] <- 1 m[m > 1 ] <- 2 or, if you have lots of intervals, something based on findInterval(). E.g., m[] <- findInterval(m, c(-Inf, 0, 1, Inf)) - 1 (What do you want to do with non-positive numbers?) Bill Dunla

Re: [R] efficient way to replace a range of numeric with a integer in a matrix

2014-08-11 Thread William Dunlap
You can use m[m > 0 & m <= 1.0] <- 1 m[m > 1 ] <- 2 or, if you have lots of intervals, something based on findInterval(). E.g., m[] <- findInterval(m, c(-Inf, 0, 1, Inf)) - 1 (What do you want to do with non-positive numbers?) Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Au

Re: [R] efficient way to replace a range of numeric with a integer in a matrix

2014-08-11 Thread Richard M. Heiberger
(m>1)+1 On Mon, Aug 11, 2014 at 5:40 PM, Jinsong Zhao wrote: > Hi there, > > I hope to replace a range of numeric in a matrix with a integer. For > example, in the following matrix, I want to use 1 to replace the elements > range from 0.0 to 1.0, and all larger than 1. with 2. > >> (m <- matrix(r

[R] efficient way to replace a range of numeric with a integer in a matrix

2014-08-11 Thread Jinsong Zhao
Hi there, I hope to replace a range of numeric in a matrix with a integer. For example, in the following matrix, I want to use 1 to replace the elements range from 0.0 to 1.0, and all larger than 1. with 2. > (m <- matrix(runif(16, 0, 2), nrow = 4)) [,1] [,2] [,3] [,4