[R] to modify a matrix
Adaikalavan Ramasamy
gisar at nus.edu.sg
Sun Feb 9 10:24:03 CET 2003
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
More information about the R-help
mailing list