And I'd like to add, just for the purpose of learning about R ... even if wishes to use the loop version, there appears to be a misunderstanding of R syntax.
The expression 1:225*100 does not produce 22500 numbers to put into the matrix, as apparently expected. Compare: > 1:3*5 [1] 5 10 15 produces 3 numbers, not 15. Operator precedence is a key concept in programming languages, and in R the colon comes before multiplication. One would have to use 1:(3*5) Continuing, > matrix(1:3*5, nrow=5, ncol=3) [,1] [,2] [,3] [1,] 5 15 10 [2,] 10 5 15 [3,] 15 10 5 [4,] 5 15 10 [5,] 10 5 15 produced a 5x3 matrix because both nrow and ncol were specified, not because the vector that was supplied has length 15. It recycles 5,10,15 as many times as needed, and recycling is another key R concept. Better is: > matrix( numeric(3*5), nrow=5) [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 [4,] 0 0 0 [5,] 0 0 0 So for the 100x225 case, and using a loop, it would be better to use D <- matrix(numeric(225*100), nrow=100) Since all the elements in the matrix are going to be replaced anyway, initializing with anything other than 0 or NA is pointless. Why start with zeroes, or whatever and then replace them? Just start with the random numbers to begin with. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/12/12 12:41 AM, "Joshua Wiley" <jwiley.ps...@gmail.com> wrote: >On Tue, Jun 12, 2012 at 12:24 AM, Rody <rodric_seu...@hotmail.com> wrote: >> I found a solution myself, but thanks for the answers. I solved it like >>this: >> D <- matrix(1:225*100,nrow=100,ncol=225) >> for(i in 1:225) >> D[,i] <- rt(100,df=225) >> end > >but as Don said, you can do this in one step (and it is both faster >and more elegant). > >D <- matrix(rt(100 * 225, df = 225), ncol = 225) > >Cheers, > >Josh > >> >> >> -- >> View this message in context: >>http://r.789695.n4.nabble.com/Storing-datasets-tp4632874p4633071.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. > > > >-- >Joshua Wiley >Ph.D. Student, Health Psychology >Programmer Analyst II, Statistical Consulting Group >University of California, Los Angeles >https://joshuawiley.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. ______________________________________________ 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.