Hi, I am trying to create a vector of length 10 (say), wherein each element will be average of random sample of size 100, from a distribution, say Normal. Can anyone please tell me without creating a "for" loop, how I can do that? Regards, -- View this message in context: http://www.nabble.com/Alternate-to-for-loop-tp22035954p22035954.html Sent from the R help mailing list archive at Nabble.com.
Try this: replicate(10, mean(rnorm(100))) On Mon, Feb 16, 2009 at 8:59 AM, megh <megh700004@yahoo.com> wrote:> > Hi, I am trying to create a vector of length 10 (say), wherein each element > will be average of random sample of size 100, from a distribution, say > Normal. Can anyone please tell me without creating a "for" loop, how I can > do that? > > Regards, > > > -- > View this message in context: > http://www.nabble.com/Alternate-to-for-loop-tp22035954p22035954.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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
megh wrote:> Hi, I am trying to create a vector of length 10 (say), wherein each element > will be average of random sample of size 100, from a distribution, say > Normal. Can anyone please tell me without creating a "for" loop, how I can > do that?Homework? Then please ask you course material or teacher. PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Uwe Ligges> Regards, > >
No, it is not homework. I obviously could do that using a for-loop, and that I already did. However I thought whether there could be a better approach as it was looking very messy and unprofessional. Uwe Ligges-3 wrote:> > > > megh wrote: >> Hi, I am trying to create a vector of length 10 (say), wherein each >> element >> will be average of random sample of size 100, from a distribution, say >> Normal. Can anyone please tell me without creating a "for" loop, how I >> can >> do that? > > > Homework? Then please ask you course material or teacher. > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > Uwe Ligges > >> Regards, >> >> > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Alternate-to-for-loop-tp22035954p22037011.html Sent from the R help mailing list archive at Nabble.com.
megh wrote:> No, it is not homework. I obviouslyFor some value of "obvious" as you has not given a single line of code as the posting guide suggests. You probably want: replicate(10, mean(rnorm(100))) Uwe Ligges> could do that using a for-loop, and that > I already did. However I thought whether there could be a better approach as > it was looking very messy and unprofessional. > > > > Uwe Ligges-3 wrote: >> >> >> megh wrote: >>> Hi, I am trying to create a vector of length 10 (say), wherein each >>> element >>> will be average of random sample of size 100, from a distribution, say >>> Normal. Can anyone please tell me without creating a "for" loop, how I >>> can >>> do that? >> >> Homework? Then please ask you course material or teacher. >> >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >> Uwe Ligges >> >>> Regards, >>> >>> >> ______________________________________________ >> 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. >> >> >
On Mon, Feb 16, 2009 at 12:59 PM, megh <megh700004 at yahoo.com> wrote:> > Hi, I am trying to create a vector of length 10 (say), wherein each element > will be average of random sample of size 100, from a distribution, say > Normal. Can anyone please tell me without creating a "for" loop, how I can > do that? > > Regards, > > > -- > View this message in context: http://www.nabble.com/Alternate-to-for-loop-tp22035954p22035954.html > Sent from the R help mailing list archive at Nabble.com. >as a variant of Patrick Burns code, you can write: rowMeans(matrix(rnorm(1000),ncol=100)) ,and substitute another distribution for rnorm if you want. /Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik