0

I am using tapply() to apply a function to my data

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)

I would like to use this within a for loop and print out the output. I have tried the following and I get the error message: 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))}
Chase
  • 67,710
  • 18
  • 144
  • 161
user1407670
  • 21
  • 1
  • 3
  • 1
    I'd also recommend avoiding using predefined R functions as variable names such as `t`. Further, the way your code is written is that `t` gets overwritten every time...which may or may not be what you think is happening. If you want `t` to have the same dimensions as your for loop, you need to preallocate the dimensions of `t`, and then iteratively fill `t` with the appropriate indexing. – Chase May 21 '12 at 15:09

1 Answers1

2

Reproducible examples make the world go round. However, your problem is that your code is not syntactically valid. If you want to put everything on a single line, you need to separate the commands by a semicolon: ;. Or, put them on two different lines. Two examples:

> 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
Community
  • 1
  • 1
Chase
  • 67,710
  • 18
  • 144
  • 161