for loop

作者: Trancer95 | 来源:发表于2018-07-18 16:26 被阅读0次

    runif(10,min=0,max=1) #产生10个最小值为0,最大值为1的随机数

    [1] 0.23848237 0.44347367 0.59288350 0.58668014 0.11930517 0.01078175

    [7] 0.10631982 0.12381276 0.42652069 0.20232110

    rnorm(10,mean=0,sd=1)  #产生10个平均值为0,方差为1的随机数

    [1]  1.1218179  0.9658383 -0.1781805 -0.2966419 -0.5140599 -0.6712930

    [7] -0.6191541  0.3281707 -0.4221563 -1.4064053

    Question :

    Now,choose one of the 50 values you have generated (it might as Ill be the firstone x1), and study what happens to the mean and median of the dataset as youchange that value. You are going to change x1 to take on, one by one, thevalues y1,…,y100, where y1=a-2(b-a) and y100=a+2(b-a), and the rest of the yiare equi-spaced in betIen(you might find the function seq() useful to generatey). This will lead you to have 100 different datasets(with the remaininggraphical display how mean and median change as the values of x1 change(Hint :a ”for loop” will be helpful.)) 

    Answer :

    ①First, I generate the sequence y.

    >y<-seq((a-2*(b-a)),(a+2*(b-a)),length.out=100)

    > y

    ②Second,I use for loop to obtain the new data. The following code show you a moredetailed result which can not only see how mean and median change as the valuesof x1 change, but also can see the value of max, min, median and sd.

    Code :

    > for(yi in y){

    +x[x=="-0.03896171"]=yi

    + z=each(max,min,mean,median,sd)(x)

    + print(z)

    + }

    相关文章

      网友评论

          本文标题:for loop

          本文链接:https://www.haomeiwen.com/subject/qbwopftx.html