Dear all, I need to do a loop in R, but I am not sure the software is generating "n" times the variables I request differently. When I ask to print the last matrix created, I just can see the loop for n=1. To be more precise, supose I need to simulate 10 times one variable and I want to fit the 10 variables simulated in a matrix. I dont really know what I am doing wrong, but I just can see 1 variable created. Below follows an example of what I want: rm(list=ls()) #remove almost everything in the memory set.seed(180185) #loop: create 10 times the variables (u1,u2,u3,u4,u5) for (i in 1:10){ u1 <- c(runif(200,0,1)) u2 <- c(runif(200,0,1)) u3 <- c(runif(200,0,1)) u4 <- c(runif(200,0,1)) u5 <- c(runif(200,0,1)) u <- c(u1,u2,u3,u4,u5) mu <- matrix(u, nrow=1000, ncol=1) } As you can see, when I print(mu), I just can see a matrix with the vector u in one column. I also tried to increase the number of columns to 10 (or i), but the matrix will have 10 times the same vector. And what I need is like a Monte Carlo simulation, where I have to simulate 10 times the variables above. Am I doing something wrong? Thanks in advance! Julia [[alternative HTML version deleted]]
Hello Julia, Your loop is just "overwriting" your variables each time. Is this the sort of thing you want ? # make a matrix with 10 cols of N random uniform values N <- 5 x <- t( replicate( 10, runif(N, 0, 1) ) ) You can leave out the t( ... ) part if you want the randomizations to be rows instead of cols Michael On 6 October 2010 20:51, Julia Lira <julia.lira at hotmail.co.uk> wrote:> > Dear all, > > > > I need to do a loop in R, but I am not sure the software is generating "n" times the variables I request differently. When I ask to print the last matrix created, I just can see the loop for n=1. > > To be more precise, supose I need to simulate 10 times one variable and I want to fit the 10 variables simulated in a matrix. I dont really know what I am doing wrong, but I just can see 1 variable created. > > Below follows an example of what I want: > > > > rm(list=ls()) #remove almost everything in the memory > > > > set.seed(180185) > > > > #loop: create 10 times the variables (u1,u2,u3,u4,u5) > > > > for (i in 1:10){ > u1 <- c(runif(200,0,1)) > u2 <- c(runif(200,0,1)) > u3 <- c(runif(200,0,1)) > u4 <- c(runif(200,0,1)) > u5 <- c(runif(200,0,1)) > u <- c(u1,u2,u3,u4,u5) > mu <- matrix(u, nrow=1000, ncol=1) > } > > > As you can see, when I print(mu), I just can see a matrix with the vector u in one column. I also tried to increase the number of columns to 10 (or i), but the matrix will have 10 times the same vector. And what I need is like a Monte Carlo simulation, where I have to simulate 10 times the variables above. > > > > Am I doing something wrong? > > > > Thanks in advance! > > > > Julia > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hi r-help-bounces at r-project.org napsal dne 06.10.2010 11:51:37:> > Dear all, > > > > I need to do a loop in R, but I am not sure the software is generating"n"> times the variables I request differently. When I ask to print the lastmatrix> created, I just can see the loop for n=1. > > To be more precise, supose I need to simulate 10 times one variable andI want> to fit the 10 variables simulated in a matrix. I dont really know what Iam> doing wrong, but I just can see 1 variable created. > > Below follows an example of what I want: > > > > rm(list=ls()) #remove almost everything in the memoryNot in memory but in file .Rdata on your PC too when you end your session> > > > set.seed(180185) > > > > #loop: create 10 times the variables (u1,u2,u3,u4,u5) > > > > for (i in 1:10){ > u1 <- c(runif(200,0,1)) > u2 <- c(runif(200,0,1)) > u3 <- c(runif(200,0,1)) > u4 <- c(runif(200,0,1)) > u5 <- c(runif(200,0,1))Five vectors with 200 numbers each despite of excessive c() construction.> u <- c(u1,u2,u3,u4,u5)vector of length 1000> mu <- matrix(u, nrow=1000, ncol=1)changed to matrix> }all made 10 times. Imagine you do a loop for (i i 10) { copy "file1" to "file2" } what do you expect as a result? Ten files or only one file? When you want a matrix with 10 colums and 1000 rows you can set it before your loop mu <- matrix(NA, nrow=1000, ncol=10) and use mu[,i] <- u as the last row in your loop. But what is wrong with mu <- matrix(runif(10000, 0,1), nrow=1000, ncol=10) for getting such matrix. Regards Petr> > > As you can see, when I print(mu), I just can see a matrix with thevector u in> one column. I also tried to increase the number of columns to 10 (or i),but> the matrix will have 10 times the same vector. And what I need is like aMonte> Carlo simulation, where I have to simulate 10 times the variables above. > > > > Am I doing something wrong? > > > > Thanks in advance! > > > > Julia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Is this homework? -- David. On Oct 6, 2010, at 5:51 AM, Julia Lira wrote:> > Dear all, > > > > I need to do a loop in R, but I am not sure the software is > generating "n" times the variables I request differently. When I ask > to print the last matrix created, I just can see the loop for n=1. > > To be more precise, supose I need to simulate 10 times one variable > and I want to fit the 10 variables simulated in a matrix. I dont > really know what I am doing wrong, but I just can see 1 variable > created. > > Below follows an example of what I want: > > > > rm(list=ls()) #remove almost everything in the memory > > > > set.seed(180185) > > > > #loop: create 10 times the variables (u1,u2,u3,u4,u5) > > > > for (i in 1:10){ > u1 <- c(runif(200,0,1)) > u2 <- c(runif(200,0,1)) > u3 <- c(runif(200,0,1)) > u4 <- c(runif(200,0,1)) > u5 <- c(runif(200,0,1)) > u <- c(u1,u2,u3,u4,u5) > mu <- matrix(u, nrow=1000, ncol=1) > } > > > As you can see, when I print(mu), I just can see a matrix with the > vector u in one column. I also tried to increase the number of > columns to 10 (or i), but the matrix will have 10 times the same > vector. And what I need is like a Monte Carlo simulation, where I > have to simulate 10 times the variables above. > > > > Am I doing something wrong? > > > > Thanks in advance! > > > > Julia > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
set.seed(180185)> > > > #loop: create 10 times the variables (u1,u2,u3,u4,u5) > > > > for (i in 1:10){ > u1 <- c(runif(200,0,1)) > u2 <- c(runif(200,0,1)) > u3 <- c(runif(200,0,1)) > u4 <- c(runif(200,0,1)) > u5 <- c(runif(200,0,1)) > u <- c(u1,u2,u3,u4,u5) > mu <- matrix(u, nrow=1000, ncol=1) > } > > > As you can see, when I print(mu), I just can see a matrix with the vector > u in one column. I also tried to increase the number of columns to 10 (or > i), but the matrix will have 10 times the same vector. And what I need is > like a Monte Carlo simulation, where I have to simulate 10 times the > variables above. > > > > Am I doing something wrong?Yes. You are not using the indexing variable, i, of the for loop to change anything each time through the loop. .
Apparently Analagous Threads
- Symbolic derivation using D in package stats - how do I properly convert the returned call into a character string?
- Simalteneous Equation Doubt in R
- data summary and some automated t.tests.
- Error while running virt-install
- Fw: Error while running virt-install