Re: [R] Change a value in a matrix randomly

2010-11-09 Thread Steve Taylor
A=matrix(0,nr=3,nc=4) A[sample(prod(dim(A)),1)]=1 >>> From: "Barroso, Judit" To:"r-help@r-project.org" Date: 10/Nov/2010 11:12a Subject: [R] Change a value in a matrix randomly I have a matrix of ceros, for example: 0 0 0 0 0 0 0

Re: [R] Change a value in a matrix randomly

2010-11-09 Thread Peter Alspach
Tena koe Judit If it is really a matrix (and not a data.frame) and you wish to change a randomly selected zero to a one, then judit <- matrix(0, nrow=3, ncol=4) judit[sample(1:length(judit), 1)] <- 1 will do. This uses the fact that a matrix is a vector with a dim attribute. HTH Peter A