Report your results for the following questions by using R.
(a) Generate 1000 data points from a normal distribution with mean 3 and sd = 1. Store the 1000 datapoints as a vector whose name is n.v. Report the first 10 elements of the vector.
(b) Calculate the average values of the first k values in n.v, where k = 1, 2, 3, 4, …, 1000. Collect the results as a vector whose name is ave.v. [Note the length of ave.v is 1000.]
这个问题是求前1个,前2个,前k+1个均值吗?用循环语句吗?
(c) Generate three figures by plotting the first 10 values, 100 values, 1000 values of ave.v. Combine the three figures into one figure, and report the figure. [Hint: Search “plot multiple figures in R” in Google.]
(d) Repeat steps 1 to 3 to generate another figure for 1000 data points from a bernouli distribution with prob = 0.4. [Hint: Check rbern]
(e) Explain What you observe in question 6(c) and 6(d).
R语言测试题-循环-求均值
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CSDN专家-HGJ 2021-09-25 10:21关注
由于要重复abc步骤,可以将上述三步封装到一个函数中,然后作图,参考以下代码:
getwd() library(Rlab) n.v<-rnorm(1000,mean=3,sd=1) f<-function(x){ ave.v=c() for (k in 1:length(x)){ ave.v<-c(ave.v,sum(x[1:k])/k) } data=matrix(c(ave.v[1:11],ave.v[1:101],ave.v[1:1001]),ncol=3) return (data) } d=f(n.v) m.v<-rbern(1000,0.4) m=f(m.v) par(mfrow=c(1,2)) matplot(d,type='b',pch=1,col=1:3) legend("topright", legend = 1:3, col=1:3, pch=1) matplot(m,type='b',pch=1,col=1:3) legend("topright", legend = 1:3, col=1:3, pch=1)
如对你有帮助,请点击一下采纳按钮。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改