Hi All. I am quite a newbie to R. This is a next basic question. I have a matrix;> x <- matrix(1:10.,5) > x[,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 I like to get a modified matrix as follows; [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 * 5 -> 40 [4,] 4 9 [5,] 5 10 The following expression does not work. if (x[x[,1]==3]) x[x[,1]==3],2]*5 How can I obtain a right expresion to get a modified matrix? Thanks in advance for help. -------========-------- mitsu5 mitsu5 at ruby.famille.ne.jp
On 02/09/03 00:04, Mitsuo Igarashi wrote:>Hi All. > >I am quite a newbie to R. >This is a next basic question. > >I have a matrix; >> x <- matrix(1:10.,5) >> x > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 >[4,] 4 9 >[5,] 5 10 > >I like to get a modified matrix as follows; > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 * 5 -> 40 >[4,] 4 9 >[5,] 5 10 > >The following expression does not work. > if (x[x[,1]==3]) x[x[,1]==3],2]*5 > >How can I obtain a right expresion to get a modified matrix?This will do it: x[,2] <- x[,2]*(1+4*(x[,1]==3)) But probably you want to do it with something like "if". So: x[,2] <- ifelse(x[,1]==3, x[,2]*5, x[,2]) Or a loop: for(i in 1:5) {if (x[,1]==3) {x[,2] <- 5*x[,2]}} -- Jonathan Baron, Professor of Psychology, University of Pennsylvania R page: http://finzi.psych.upenn.edu/
Dear Mitsuo, It isn't completely clear to me what the criterion is for selecting elements, but if you want to change 8 to 40, but aren't sure where 8 is (or 8s are), then the following will work: x[x==8] <- 40 The trick here is to index the matrix as a vector (since matrices in R are vectors with a dim attribute). More generally, there's a great deal of free information available about how to use R that can help you to answer questions like this, including manuals supplied with the R distribution and other material available from the R web site. John At 12:04 AM 2/9/2003 +0900, Mitsuo Igarashi wrote:>Hi All. > >I am quite a newbie to R. >This is a next basic question. > >I have a matrix; > > x <- matrix(1:10.,5) > > x > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 >[4,] 4 9 >[5,] 5 10 > >I like to get a modified matrix as follows; > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 * 5 -> 40 >[4,] 4 9 >[5,] 5 10 > >The following expression does not work. > if (x[x[,1]==3]) x[x[,1]==3],2]*5 > >How can I obtain a right expresion to get a modified matrix?----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox -----------------------------------------------------
May I suggest you learn the handy function called which(). yes.rows <- which(x[,1]==3) # Row that meets this condition x[ yes, 2] <- x[ yes.rows, 2] * 5 # Perform the desired change for the row that meets this condition. At 12:04 AM 2/9/2003 +0900, Mitsuo Igarashi wrote:>Hi All. > >I am quite a newbie to R. >This is a next basic question. > >I have a matrix; > > x <- matrix(1:10.,5) > > x > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 >[4,] 4 9 >[5,] 5 10 > >I like to get a modified matrix as follows; > [,1] [,2] >[1,] 1 6 >[2,] 2 7 >[3,] 3 8 * 5 -> 40 >[4,] 4 9 >[5,] 5 10 > >The following expression does not work. > if (x[x[,1]==3]) x[x[,1]==3],2]*5 > >How can I obtain a right expresion to get a modified matrix?----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- ______________________________________________ R-help at stat.math.ethz.ch mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help