Dear R expert, The following is my questions: 1. How to generate two sequences of 150 uniform deviates, called v1 and v2, in the range [-1,1]. 2. How to compute r=(v1)^2+(v2)^2 3.If r is outside the range of (0,1) then it will be discarded. 4 How to compute (v1)*sqrt(-2*log(r)/r) and output the first 100 z values in the list. 5. Plot histograms for the sequences from steps 1 and 2. Thanks very much for your help! xingyu
Firstly, these are all very basic questions, thah you coukld probobly answer yourself by searching the manual and help files. Nevertheless, here are the answers to give you a little head start.> Dear R expert, > The following is my questions: > > 1. How to generate two sequences of 150 uniform > deviates, called v1 and v2, in the range [-1,1].v1<-runif(n=150,min=-1,max=1) v2<-runif(n=150,min=-1,max=1)> 2. How to compute r=(v1)^2+(v2)^2r<-(v1)^2+(v2)^2> 3.If r is outside the range of (0,1) then it will be > discarded.newr<-r[r<1] newr<-newr[r>0]> 4 How to compute (v1)*sqrt(-2*log(r)/r) and output the > first 100 z values in the list.z<-(v1)*sqrt(-2*log(r)/r) z[1:100] #or print(z[1:100])> 5. Plot histograms for the sequences from steps 1 and > 2.hist(v1) hist(v2) hist(r)> > Thanks very much for your help!I hope this helps, Ales Ziberna> > xingyu > > ______________________________________________ > R-help at 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
From: Ales Ziberna> > Firstly, these are all very basic questions, thah you coukld > probobly answer > yourself by searching the manual and help files. > Nevertheless, here are the > answers to give you a little head start. > > > Dear R expert, > > The following is my questions: > > > > 1. How to generate two sequences of 150 uniform > > deviates, called v1 and v2, in the range [-1,1]. > v1<-runif(n=150,min=-1,max=1) > v2<-runif(n=150,min=-1,max=1) > > > 2. How to compute r=(v1)^2+(v2)^2 > r<-(v1)^2+(v2)^2 > > > 3.If r is outside the range of (0,1) then it will be > > discarded. > newr<-r[r<1] > newr<-newr[r>0]I guess you meant newr <- newr[newr > 0] for the second line above? I would do it in one line as newr <- r[abs(r - 0.5) < 0.5] Andy> > 4 How to compute (v1)*sqrt(-2*log(r)/r) and output the > > first 100 z values in the list. > z<-(v1)*sqrt(-2*log(r)/r) > z[1:100] #or > print(z[1:100]) > > > 5. Plot histograms for the sequences from steps 1 and > > 2. > hist(v1) > hist(v2) > hist(r) > > > > > Thanks very much for your help! > I hope this helps, > > Ales Ziberna > > > > > xingyu > > > > ______________________________________________ > > R-help at 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 > > ______________________________________________ > R-help at 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 > >