Is there any routine to generate for one vector matrix. If I have X I want to generate start from zero to maximum value each vector. For example, I have a vector x = (4,2,3,1,4) I want to generate n=6 times, for 4, start 0 to 4, then 2 start 0 to 2, ect. The result something like this: generate(x,n=6) 1,1,2,1,4 1,2,3,0,3 4,0,1,1,1 3,1,0,1,4 0,0,3,0,0 4,1,3,0,4 Could anyone help me. Thanks. Regards, Jan Sabee [[alternative HTML version deleted]]
On Thu, 2005-10-13 at 19:47 +0200, Jan Sabee wrote:> Is there any routine to generate for one vector matrix. > If I have X I want to generate start from zero to maximum value each vector. > > For example, I have a vector x = (4,2,3,1,4) > I want to generate n=6 times, for 4, start 0 to 4, then 2 start 0 to 2, ect. > > The result something like this: > > generate(x,n=6) > 1,1,2,1,4 > 1,2,3,0,3 > 4,0,1,1,1 > 3,1,0,1,4 > 0,0,3,0,0 > 4,1,3,0,4 > > Could anyone help me. Thanks. > Regards, > Jan SabeeIf 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:> x <- c(4, 2, 3, 1, 4)> x[1] 4 2 3 1 4> sapply(x, function(x) sample(0:x, 6, replace = TRUE))[,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
A not-so-clever solution: n <- 6 x <- c(4,2,3,1,4) y <- matrix(0, nrow=n, ncol=length(x)) for (i in 1:n){ for (j in 1:length(x)){ y[i,j] <- round(runif(1,min=0,max=x[j]))}} Hope it helps Xiaohua On 10/13/05, Jan Sabee <jan.sabee at gmail.com> wrote:> Is there any routine to generate for one vector matrix. > If I have X I want to generate start from zero to maximum value each vector. > > For example, I have a vector x = (4,2,3,1,4) > I want to generate n=6 times, for 4, start 0 to 4, then 2 start 0 to 2, ect. > > The result something like this: > > generate(x,n=6) > 1,1,2,1,4 > 1,2,3,0,3 > 4,0,1,1,1 > 3,1,0,1,4 > 0,0,3,0,0 > 4,1,3,0,4 > > Could anyone help me. Thanks. > Regards, > Jan Sabee > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Xiaohua Dai, Dr. Postdoc in elephant-vegetation ecosystem simulation Centre for Systems Research, Durban Institute of Technology P.O.Box 953, Durban 4000, South Africa