全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
629 17
2013-08-31
悬赏 60 个论坛币 已解决
这里有个英文R 编程问题。

Use for loop and y <- rnorm(100) to generate a random sample of 100 numbers from a normal distribution. Calculate the mean and standard deviation of y. Now put the calculation in a loop and repeat 25 times. Store the 25 means in a vector named av. To do that, you need to define a vector of NAs of length 25, using the R command av <- rep(NA, 25), before the for loop Calculate the standard deviation of the values in av.

楼主只是懂一点点R 所以codes里有问题,无法产生题目中要求的结果。

av <- rep(NA, 25)
for (i in 1:25)
{ y <- rnorm(100)
  av <- mean(y)}
sd(av)

期待高手帮忙解答。

最佳答案

肉肠 查看完整内容

很简单,你现在的问题在于每次loop没有把数值放在特定的哪个格子里面 av
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-8-31 09:12:50
很简单,你现在的问题在于每次loop没有把数值放在特定的哪个格子里面

av <- rep(NA, 25)
for (i in 1:25)
{ y <- rnorm(100)
  av [ i ] <- mean(y)}
sd(av)

就可以了
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-8-31 09:14:25
虽然我不懂,帮忙顶一下
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-8-31 09:21:33
猫如此有柴啊?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-8-31 09:23:07
还有,你得把中间[ i ]那个空格删掉,因为在论坛里不加空格好像打不出来
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-8-31 09:25:15
#define the av;
av<-rep(NA, 25);
#define the standard deviation;
std<-rep(NA,25);
for (i in 1:25){
        y<-rnorm(100)
        av[i]<-mean(y)
        std[i]<-sd(y)
}
#print the av
av
#print the standard deviation
std
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群