[R] Vectors, Loops, Rnorm and KS-Testing

Ivan Calandra ivan.calandra at univ-reims.fr
Mon May 23 16:56:03 CEST 2016

Hi Nicoletta,

You need to read more introductory material.
One of the problems with your code is that you actually don't create any 
value, let alone any vector; you just display it with cat().
Then you try to create each value of your vector with an explicit for 
loop, but you don't need to; replicate() does the job in a compact code.

So if I understand correctly what you want to achieve, I would do this:
kolmo <- function(x, meanR=0, sdR=1, repet=1){
     out <- replicate(repet, mean(rnorm(x, meanR, sdR)))
# I changed the names of your arguments to make it less confusing (but I 
wasn't imaginative)
# I set some default values for 'meanR' and 'sdR', which are actually 
the defaults of rnorm()
# 'return(out)' is not necessary but I prefer to explicitly write what I 
want the function to output

kolmo(x=10, repet=3)


Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr

Le 23/05/2016 à 11:30, Nicoletta Sabov a écrit :
> Hi there,
> I need a function, that calculates the mean of a desired amount of normally distributed numbers and repeats this process for a desired number of repetitions. The function should return only a single vector consisting solely of the calculated means. Also the user of this function must be able to specify the parameters of the normal distribution used while calculating the means. I need this function for the Kolmogorov Smirnov Test.
> I am a total beginner, so far I only have come to this point:
> kolmo <- function(x, mean, sd, rep){
>    for(i in rep){
>      cat(mean(rnorm(x, mean, sd)))
> }}
> This returns a simple number, but I do need a vector to be returned.
> Thanks a lot in advance!
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

More information about the R-help mailing list