0

我正在使用 tapply() 将函数应用于我的数据

Myrepfun <- function(x,n){
    nstudents <- replicate(1000,sum(sample(x, size=n,replace=TRUE)))
    quantile(nstudents,probs=0.95)
}

tapply(weight,schoolcode,Myrepfun,n=2)

我想在 for 循环中使用它并打印输出。我尝试了以下方法并收到错误消息:Error: unexpected symbol in "for(n in 12:13) (t=tapply(ow,sc,ndropfunction,n,p=0.95) output

for(n in 1:25) {t=tapply(weight,schoolcode,Myrepfun,n,p=0.95) print(c=(t,n))}
4

1 回答 1

2

可重复的例子让世界运转起来。但是,您的问题是您的代码在语法上无效。如果要将所有内容放在一行中,则需要用分号分隔命令:;。或者,将它们放在两条不同的线上。两个例子:

> x <- runif(100)
> for (i in 1:3){out <- mean(x);print(c(out,i))}
#-----
[1] 0.4958944 1.0000000
[1] 0.4958944 2.0000000
[1] 0.4958944 3.0000000
> for (i in 1:3){
+   out <- mean(x)
+   print(c(out,i))
+ }
#-----
[1] 0.4958944 1.0000000
[1] 0.4958944 2.0000000
[1] 0.4958944 3.0000000
于 2012-05-21T14:54:20.847 回答