Skip to content
Prev 263196 / 398502 Next

matrix problem

How about:

y <- c(1,1,1,3,2)
m <- matrix(0, nrow=length(y), ncol=4)
m[y==1, ] <- matrix(1:4, nrow=sum(y == 1), ncol=4, byrow=TRUE)

or, depending on your actual problem
y <- c(1,1,1,3,2)
m <- matrix(0, nrow=length(y), ncol=4)
m[y == 1,] <- col(m[y == 1,])

Sarah
On Mon, Jun 20, 2011 at 3:54 PM, Costis Ghionnis <conighion at gmail.com> wrote: