[R] manipulating elements of a matrix

Spencer Graves spencer.graves at pdf.com
Fri May 9 16:53:24 CEST 2003


	  Since you mentioned "lapply", I wondered if you had considered the 
following:

A0 <- array(c(0, 0, 2, 3, 1, 1, 1, 0, 1, 0, 0, 0),
	dim=c(4, 3))
ind0 <- rep(1:4, pmax(1, A0[,1]))
A0[ind0,]

	  With a bit more messing about, I believe I could change "0 1 1" to "0 
1 0 / 0 0 1".  However, I could not find a two-line solution, so I'll 
leave it for you.

hth.  spencer graves

Vumani Dlamini wrote:
> Dear R users:
> 
> I have the following matrix.
> 
> 0 1 1
> 0 1 0
> 2 1 0
> 3 0 0
> 
> I would like to spread the matrix such that whenever the row sum is 
> greater than 1 the row is repeated the number of times given by the row 
> sum. Furthermore I would like to split the following cases:
> 
> 0 1 1
> such that it map to the following matrix
> 
> 0 1 0
> 0 0 1
> 
> such that each row adds up to 1.
> 
> I have no problems with cases like c(3,0,0) using "lapply" but I have no 
> splitting cases of the form c(1,1,0) and thats where I need your help.
> 
> Thanking you as always.
> 
> 
> Vumani Dlamini
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list