RE: [R] to modify a matrix

About this list Date view Thread view Subject view Author view Attachment view

From: Adaikalavan Ramasamy (gisar@nus.edu.sg)
Date: Sun 09 Feb 2003 - 20:23:10 EST


Message-id: <024D6AEFCB92CB47BA1085751D184BB801053EF0@MBXSRV03.stf.nus.edu.sg>

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@mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:21 EST