[R] Storing output vector form a loop as a matrix
Based on help files and searching the archives and help from the listserv I have managed to build my monte carlo program. However I cannot get the proper syntax for running the loop and storing the output of each loop (which is a vector) into a matrix. I took out some of the crazy code I was writing, but here is what I have: rm(list = ls(all = TRUE)) # removes everything a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) c -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE a - sample(a, replace=TRUE) c -data.frame(b,a) output.vector - by(c, a, function(x) sum(x$b)) output.vector - as.vector(output) output.vector - data.frame(output) #END LOOP here -- What I would like to have at the end is the output.matrix contain as a column the output.vector from each iteration. The actual data frame I will be running has 60,000 observations and I am going to run 2 iterations so speed is important too. Thanks so much EG [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Storing output vector form a loop as a matrix
I'm not sure I've completely understood this, but would this do what you want: for (i in 1:N){ aa - sample(a, replace=TRUE) c -data.frame(b,aa) output - by(c, aa, function(x) sum(x$b)) output.matrix[,i] - as.vector(output) } On 17/06/07, Economics Guy [EMAIL PROTECTED] wrote: Based on help files and searching the archives and help from the listserv I have managed to build my monte carlo program. However I cannot get the proper syntax for running the loop and storing the output of each loop (which is a vector) into a matrix. I took out some of the crazy code I was writing, but here is what I have: rm(list = ls(all = TRUE)) # removes everything a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) c -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE a - sample(a, replace=TRUE) c -data.frame(b,a) output.vector - by(c, a, function(x) sum(x$b)) output.vector - as.vector(output) output.vector - data.frame(output) #END LOOP here -- What I would like to have at the end is the output.matrix contain as a column the output.vector from each iteration. The actual data frame I will be running has 60,000 observations and I am going to run 2 iterations so speed is important too. Thanks so much EG [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Storing output vector form a loop as a matrix
That gives: Error in output.matrix[, i] - as.vector(output.vector) : number of items to replace is not a multiple of replacement length But I think that is close. Thanks, EG On 6/17/07, David Barron [EMAIL PROTECTED] wrote: I'm not sure I've completely understood this, but would this do what you want: for (i in 1:N){ aa - sample(a, replace=TRUE) c -data.frame(b,aa) output - by(c, aa, function(x) sum(x$b)) output.matrix[,i] - as.vector(output) } On 17/06/07, Economics Guy [EMAIL PROTECTED] wrote: Based on help files and searching the archives and help from the listserv I have managed to build my monte carlo program. However I cannot get the proper syntax for running the loop and storing the output of each loop (which is a vector) into a matrix. I took out some of the crazy code I was writing, but here is what I have: rm(list = ls(all = TRUE)) # removes everything a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) c -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE a - sample(a, replace=TRUE) c -data.frame(b,a) output.vector - by(c, a, function(x) sum(x$b)) output.vector - as.vector(output) output.vector - data.frame(output) #END LOOP here -- What I would like to have at the end is the output.matrix contain as a column the output.vector from each iteration. The actual data frame I will be running has 60,000 observations and I am going to run 2 iterations so speed is important too. Thanks so much EG [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Storing output vector form a loop as a matrix
I think this does it for you: a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) C -data.frame(a,b) # don't use 'c' as it is a primitive function - canbe confusing N=10 #Number of Loops output.matix - replicate(N, function(x){ + .samp - C[sample(nrow(C), replace=TRUE), ] + tapply(.samp$b, .samp$a, sum) + }) # transpose if you want samples in columns output.matrix - t(output.matrix) output.matrix [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] A 10 130 100 40 50 40 30 50 5050 B 130 60 50 110 100 160 110 130 60 150 C 40 NA 60 80 120 60 NA 60 8020 On 6/17/07, Economics Guy [EMAIL PROTECTED] wrote: Based on help files and searching the archives and help from the listserv I have managed to build my monte carlo program. However I cannot get the proper syntax for running the loop and storing the output of each loop (which is a vector) into a matrix. I took out some of the crazy code I was writing, but here is what I have: rm(list = ls(all = TRUE)) # removes everything a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) c -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE a - sample(a, replace=TRUE) c -data.frame(b,a) output.vector - by(c, a, function(x) sum(x$b)) output.vector - as.vector(output) output.vector - data.frame(output) #END LOOP here -- What I would like to have at the end is the output.matrix contain as a column the output.vector from each iteration. The actual data frame I will be running has 60,000 observations and I am going to run 2 iterations so speed is important too. Thanks so much EG [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Storing output vector form a loop as a matrix
Thanks so much to both of you. I did not exactly use the code from either of you but your post help me figure out the things I was doing wrong. For anyone who stumbles upon this thread later, here is the code I used that made it work: Thanks Again. a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) d -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE for (i in 1:N){ a - sample(a, replace=FALSE) d -data.frame(b,a) output.vector - by(d, a, function(x) sum(x$b)) output.vector - as.vector(output.vector) output.vector - data.frame(output.vector) output.vector - as.vector(output.vector) #test output.matrix[,i] - t(output.vector) } On 6/17/07, jim holtman [EMAIL PROTECTED] wrote: I think this does it for you: a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) C -data.frame(a,b) # don't use 'c' as it is a primitive function - canbe confusing N=10 #Number of Loops output.matix - replicate(N, function(x){ + .samp - C[sample(nrow(C), replace=TRUE), ] + tapply(.samp$b, .samp$a, sum) + }) # transpose if you want samples in columns output.matrix - t(output.matrix) output.matrix [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] A 10 130 100 40 50 40 30 50 5050 B 130 60 50 110 100 160 110 130 60 150 C 40 NA 60 80 120 60 NA 60 8020 On 6/17/07, Economics Guy [EMAIL PROTECTED] wrote: Based on help files and searching the archives and help from the listserv I have managed to build my monte carlo program. However I cannot get the proper syntax for running the loop and storing the output of each loop (which is a vector) into a matrix. I took out some of the crazy code I was writing, but here is what I have: rm(list = ls(all = TRUE)) # removes everything a -c(A,C,B,A,B,C) b -c(10,20,30,40,50,60) c -data.frame(a,b) N=10 #Number of Loops output.matrix - matrix(0.0,3,N) #I need to START LOOP HERE a - sample(a, replace=TRUE) c -data.frame(b,a) output.vector - by(c, a, function(x) sum(x$b)) output.vector - as.vector(output) output.vector - data.frame(output) #END LOOP here -- What I would like to have at the end is the output.matrix contain as a column the output.vector from each iteration. The actual data frame I will be running has 60,000 observations and I am going to run 2 iterations so speed is important too. Thanks so much EG [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.