Thanks for everyone.
I think the approach below is most suitable for me, as a beginner. x=list() > > for(i in 1:n){ > > x[[i]]=rnorm(i,0,1) > > } Now, I am trying to obtain the sample variance (S^2) of the 1000 samples that I have generated before. I am wondering what command I should use in order to get the sample variance for all the 1000 samples. What I am capable of doing now is just typing in var(z[[1]]) var(z[[2]])..................... Thanks for help. Debbie > Date: Thu, 14 May 2009 08:26:03 +0200 > From: waclaw.marcin.kusnierc...@idi.ntnu.no > To: b.rowling...@lancaster.ac.uk > CC: r-help@r-project.org > Subject: Re: [R] Simulation > > Barry Rowlingson wrote: > > On Wed, May 13, 2009 at 9:56 PM, Wacek Kusnierczyk > > <waclaw.marcin.kusnierc...@idi.ntnu.no> wrote: > > > >> Barry Rowlingson wrote: > >> > > > > > >> n = 1000 > >> benchmark(columns=c('test', 'elapsed'), order=NULL, > >> 'for'={ l = list(); for (i in 1:n) l[[i]] = rnorm(i, 0, 1) }, > >> lapply=lapply(1:n, rnorm, 0, 1) ) > >> # test elapsed > >> # 1 for 9.855 > >> # 2 lapply 8.923 > >> > >> > >> > >>> Yes, you can probably vectorize this with lapply or something, but I > >>> prefer clarity over concision when dealing with beginners... > >>> > >> but where's the preferred clarity in the for loop solution? > >> > > > > Seriously? You think: > > > > lapply(1:n, rnorm, 0, 1) > > > > is 'clearer' than: > > > > x=list() > > for(i in 1:n){ > > x[[i]]=rnorm(i,0,1) > > } > > > > for beginners? > > > > seriously, i do; but it does depend on who those beginners are. if > they come directly from c and the like, you're probably right. > > > > Firstly, using 'lapply' introduces a function (lapply) that doesn't > > have an intuitive name. Also, it takes a function as an argument. The > > concept of having a function as a parameter to another function is > > something that a lot of programming beginners have trouble with - > > unless they were brought up on LISP of course, and few of us are. > > > > well, that's one of the first things you learn on a programming > languages course that is not procedural programming-{centered,biased}. > no need for prior lisp experience. if messing with closures in not > involved (as here), no need for advanced discussion is needed. > > also, the for looping may not be as trivial stuff to explain as you > might think. note, you're talking about r, not c, and the treatment of > iterator variables in for loops in scripting languages differs: > > perl -e ' > $i = 0; > for $i (1..5) { # iterate with $i > }; > print "$i\n" ' > # 0 > > ruby -e ' > i = 0 > for i in 1..5 # iterate with i > end > printf "%d\n", i ' > # 5 > > and you've gotten into explaining lexical scoping etc. > > > > I propose that the for-loop example is clearer to a larger population > > than the lapply version. > > which population have you sampled from? you may not be wrong, but give > some data. > > > > Plus it's only useful in that form if the > > first parameter is the one you want to lapply over. If you want to > > work over the third parameter, say, you then need: > > > > lapply(1:n,function(i){rnorm(100,0,i)}) > > > > at which point you've introduced anonymous functions. The jump from: > > > > x[[i]] = rnorm(i,0,1) > > to > > x[[i]] = rnorm(100,0,i) > > > > is much less than the changes in the lapply version, where you have to > > go 'oh hang on, lapply only works on the first argument, so you have > > to write another function, but you can do that inline like this...'. > > > > you may be unhappy to learn that you're unaware of how the lapply > solution can still be nicely adapted here: > > lapply(1:n, rnorm, n=100, mean=0) > > > Okay, maybe my example is a little contrived, but I still think for a > > beginners context it's important not to jump too many paradigms at a > > time. > > > > for a complete beginner, jump into for loops may not be that trivial as > you seem to think. there's still quite some stuff to be explained to > clarify that > > i = 0 > for (i in 1:n) > # do stuff > print(i) > > will print n, not 0. unless n=0, of course. > > vQ > > ______________________________________________ > 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. _________________________________________________________________ Looking to change your car this year? Find car news, reviews and more e%2Ecom%2Fcgi%2Dbin%2Fa%2Fci%5F450304%2Fet%5F2%2Fcg%5F801459%2Fpi%5F1004813%2Fai%5F859641&_t=762955845&_r=tig_OCT07&_m=EXT [[alternative HTML version deleted]] ______________________________________________ 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.