Re: [R] matrix - change values
A matrix is already a vector, you don't need to do the transformations, just do the same thing directly: > tmp <- matrix( sample(1:12), ncol=3 ) > tmp [,1] [,2] [,3] [1,] 1116 [2,]379 [3,]4 128 [4,]25 10 > tmp[tmp > 5] <- 0 > tmp [,1] [,2] [,3] [1,]010 [2,]300 [3,]400 [4,]250 If on the other hand, your matrix is really a data frame then functions like lapply, sapply, transform may help. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Thursday, December 14, 2006 7:01 AM To: r-help@stat.math.ethz.ch Subject: [R] matrix - change values Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a<-c(1:10); a[a>5]<-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@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. __ R-help@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.
Re: [R] matrix - change values
I would like to thanks everybody for helpful suggestion. Rob Od: [EMAIL PROTECTED] Do: r-help@stat.math.ethz.ch Data: 14 grudnia 2006 15:01 Temat: [R] matrix - change values > Dear R Users, > I have a matrix A, and I want to change every value of this matrix if these > values are greater than an assuming value. For a vector it is simple, e.g. > a<-c(1:10); a[a>5]<-0. > Of course, I can change matrix to vector, assign a value then change vector > to matrix. But does there exist simpler way? > Any suggestion are appreciate. > Rob > > __ > R-help@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. __ R-help@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.
Re: [R] matrix - change values
Rob, Try a[a>5]<-0 Yup. It works for matrices (and for arrays). It also works with the replacement value being a vector. For example, try b <- array(1:24, dim=c(3, 4, 2)) b[(b>8) & (b<17)] <- 101:108 I think the reason it works like this is that internally array are stored as vectors. Cheers, Andy __ Andy Jaworski 518-1-01 Process Laboratory 3M Corporate Research Laboratory - E-mail: [EMAIL PROTECTED] Tel: (651) 733-6092 Fax: (651) 736-3122 [EMAIL PROTECTED] 2.pl Sent by: To [EMAIL PROTECTED] r-help@stat.math.ethz.ch at.math.ethz.chcc Subject 12/14/2006 08:01 [R] matrix - change values AM Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a<-c(1:10); a[a>5]<-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@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. __ R-help@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.
Re: [R] matrix - change values
[EMAIL PROTECTED] wrote: > Dear R Users, > I have a matrix A, and I want to change every value of this matrix if these > values are greater than an assuming value. For a vector it is simple, e.g. > a<-c(1:10); a[a>5]<-0. > Of course, I can change matrix to vector, assign a value then change vector > to matrix. But does there exist simpler way? The same syntax as for a vector: A[A>5] <- 0 Remember that matrices are just vectors with a dim attribute. The dim attribute is unchanged by this operation: > A <- matrix(1:10, 2, 5) > A [,1] [,2] [,3] [,4] [,5] [1,]13579 [2,]2468 10 > A[A>5] <- 0 > A [,1] [,2] [,3] [,4] [,5] [1,]13500 [2,]24000 Duncan Murdoch __ R-help@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.
[R] matrix - change values
Dear R Users, I have a matrix A, and I want to change every value of this matrix if these values are greater than an assuming value. For a vector it is simple, e.g. a<-c(1:10); a[a>5]<-0. Of course, I can change matrix to vector, assign a value then change vector to matrix. But does there exist simpler way? Any suggestion are appreciate. Rob __ R-help@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.