Hi Everyone, I have a 2-dim data.matrix(e.g., table1) in which row1 specifies a range of values. row2 - rown specify the number of times I want to replicate each corresponding value in row1. I can do this with the following function: rep(c(table1[1,]),c(table1[X,])) #where X would go from 2 - n. Now, I can do this manually by changing the values of X and save each resulting array/vector in an object, or write a for loop that will iterate through the rows and output a new data.matrix in which row1 - rown will correspond to the vectors generated by replicating the values of row1 "row2 - rown" independent times from the original data.matrix with the rep function shown above. So far I have been unable to get the for loop right. Any help will be most appreciated! Thanks beforehand for your help. Best, A -- View this message in context: http://r.789695.n4.nabble.com/for-loop-help-please-tp2534666p2534666.html Sent from the R help mailing list archive at Nabble.com.
I do not follow. Could you please provide a small reproducible example of what "table1" might look like, and what you want as a result? Surely you don't need a for loop. alfredo wrote:> Hi Everyone, > > I have a 2-dim data.matrix(e.g., table1) in which row1 specifies a range of > values. row2 - rown specify the number of times I want to replicate each > corresponding value in row1. I can do this with the following function: > > rep(c(table1[1,]),c(table1[X,])) #where X would go from 2 - n. > > Now, I can do this manually by changing the values of X and save each > resulting array/vector in an object, or write a for loop that will iterate > through the rows and output a new data.matrix in which row1 - rown will > correspond to the vectors generated by replicating the values of row1 "row2 > - rown" independent times from the original data.matrix with the rep > function shown above. So far I have been unable to get the for loop right. > > Any help will be most appreciated! Thanks beforehand for your help. > > Best, > > A
Hi A, Here is a little example that I believe does what you want. I am not quite sure how you want all the output in a new matrix, because as you repeat each value is the first row varying numbers of times, you will not have rows of equal length. Although perhaps your data is setup so that you can. # Sample data table1 <- matrix(1:16, ncol = 4) table1 # look at it # Using your code to create the first example rep(table1[1,], table1[2,]) # Using apply() to go through every row of the matrix 'table1' # except the first ([-1, ]) apply(table1[-1, ], 1, function(x) {rep(table1[1,], x)}) # Using a for loop to do the same for(i in 2:nrow(table1)) { print(rep(table1[1, ], table1[i, ])) } # In general, I think apply() is prettier # It is also easier to assign all the output to a list # Compared to using a for loop Best regards, Josh On Fri, Sep 10, 2010 at 8:54 AM, alfredo <alfredotello at gmail.com> wrote:> > Hi Everyone, > > I have a 2-dim data.matrix(e.g., table1) in which row1 specifies a range of > values. row2 - rown specify the number of times I want to replicate each > corresponding value in row1. I can do this with the following function: > > rep(c(table1[1,]),c(table1[X,])) #where X would go from 2 - n. > > Now, I can do this manually by changing the values of X and save each > resulting array/vector in an object, or write a for loop that will iterate > through the rows and output a new data.matrix in which row1 - rown will > correspond to the vectors generated by replicating the values of row1 "row2 > - rown" independent times from the original data.matrix with the rep > function shown above. So far I have been unable to get the for loop right. > > Any help will be most appreciated! Thanks beforehand for your help. > > Best, > > A > -- > View this message in context: http://r.789695.n4.nabble.com/for-loop-help-please-tp2534666p2534666.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
try this:> x <- matrix(sample(25,25), 5) > x[,1] [,2] [,3] [,4] [,5] [1,] 12 24 14 3 20 [2,] 21 7 5 15 17 [3,] 11 10 22 16 9 [4,] 6 25 4 1 23 [5,] 2 19 8 13 18> # save result in a list > result <- lapply(2:nrow(x), function(.row){+ rep(x[1,], times=x[.row,]) + })> result[[1]] [1] 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 24 24 24 24 24 24 24 14 14 14 14 14 3 3 [36] 3 3 3 3 3 3 3 3 3 3 3 3 3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 [[2]] [1] 12 12 12 12 12 12 12 12 12 12 12 24 24 24 24 24 24 24 24 24 24 14 14 14 14 14 14 14 14 14 14 14 14 14 14 [36] 14 14 14 14 14 14 14 14 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 20 20 20 20 20 20 20 20 20 [[3]] [1] 12 12 12 12 12 12 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 14 14 14 14 [36] 3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 [[4]] [1] 12 12 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 14 14 14 14 14 14 14 14 3 3 3 3 3 3 [36] 3 3 3 3 3 3 3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20>On Fri, Sep 10, 2010 at 11:54 AM, alfredo <alfredotello at gmail.com> wrote:> > Hi Everyone, > > I have a 2-dim data.matrix(e.g., table1) in which row1 specifies a range of > values. row2 - rown specify the number of times I want to replicate each > corresponding value in row1. I can do this with the following function: > > rep(c(table1[1,]),c(table1[X,])) #where X would go from 2 - n. > > Now, I can do this manually by changing the values of X and save each > resulting array/vector in an object, or write a for loop that will iterate > through the rows and output a new data.matrix in which row1 - rown will > correspond to the vectors generated by replicating the values of row1 "row2 > - rown" independent times from the original data.matrix with the rep > function shown above. So far I have been unable to get the for loop right. > > Any help will be most appreciated! Thanks beforehand for your help. > > Best, > > A > -- > View this message in context: http://r.789695.n4.nabble.com/for-loop-help-please-tp2534666p2534666.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hi: It's not immediately clear what you have in mind, as others have noted, but here are a couple of ideas that seem as though they may apply to your problem, as dangerous as it is to play clairvoyant: I'm using vectors instead of a matrix, but the first vector, val, contains the values whereas the second, reps, holds the number of desired repetitions, which are randomly generated to take values from 1:5. val <- 1:10 reps <- reps = sample(1:5, 10, replace = TRUE) (1) Output is a vector: rep(val, reps) [1] 1 1 1 2 2 3 3 3 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 8 9 [26] 9 10 10 10 10 10 (2) Output is a list: l <- mapply(rep, val, reps)> l[[1]] [1] 1 1 1 [[2]] [1] 2 2 [[3]] [1] 3 3 3 [[4]] [1] 4 4 4 [[5]] [1] 5 5 5 5 5 [[6]] [1] 6 6 6 6 6 [[7]] [1] 7 7 [[8]] [1] 8 [[9]] [1] 9 9 [[10]] [1] 10 10 10 10 10 Hopefully, one of these answers your question. If not, you'll need to explain your problem in more detail. The easiest way to do this is to come up with a toy example, what you tried that didn't work, and the expected outcome. Dennis On Fri, Sep 10, 2010 at 8:54 AM, alfredo <alfredotello@gmail.com> wrote:> > Hi Everyone, > > I have a 2-dim data.matrix(e.g., table1) in which row1 specifies a range of > values. row2 - rown specify the number of times I want to replicate each > corresponding value in row1. I can do this with the following function: > > rep(c(table1[1,]),c(table1[X,])) #where X would go from 2 - n. > > Now, I can do this manually by changing the values of X and save each > resulting array/vector in an object, or write a for loop that will iterate > through the rows and output a new data.matrix in which row1 - rown will > correspond to the vectors generated by replicating the values of row1 "row2 > - rown" independent times from the original data.matrix with the rep > function shown above. So far I have been unable to get the for loop right. > > Any help will be most appreciated! Thanks beforehand for your help. > > Best, > > A > -- > View this message in context: > http://r.789695.n4.nabble.com/for-loop-help-please-tp2534666p2534666.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]