Skip to content
Prev 78974 / 398502 Next

How to generate for one vector matrix

On Thu, 2005-10-13 at 19:47 +0200, Jan Sabee wrote:
If I am properly understanding what you are doing here, you have an
initial vector of values. You want to create a matrix, whose columns are
the result of random sampling with replacement from the initial vector
'n' times, where the sampling space for each column "i" is from 0:x[i]?

If correct, this should do it:
[1] 4 2 3 1 4
[,1] [,2] [,3] [,4] [,5]
[1,]    4    2    1    1    2
[2,]    3    0    0    0    0
[3,]    1    2    0    1    3
[4,]    4    2    1    1    1
[5,]    3    1    3    1    0
[6,]    0    1    1    1    0


Just replace '6' in the sample() arguments with the 'n' you require.

See ?sapply and ?sample.

HTH,

Marc Schwartz