0

I'm having a bit of trouble with a function i am writing. I have one set of values I want to put in the first column of my prescribed b x 2 matrix and another set of values I want to put in the second column. Here is my code so that it makes more sense

sampl=function(v,b) {
  output=matrix(0,ncol=2,nrow=b)
  sumj=0
  av=0
  n=length(v)
  for (i in 1:b) {
    for (j in 1:n) {
      k=sample(v,n,replace=T)
      av[i]=mean(k)
      sumj[i]=sum((k[j]-mean(k))^2)
      output[i,1]=sumj[i]
      output[i,2]=av[i]
    }
  }
  output
}

but this is coming up with

Error in `[<-`(`*tmp*`, i, 2, value = -1.88) : subscript out of bounds

output[i,1] seems to work but output[i,2] seems to raise the error up. your help would be appreciated

nicola
  • 24,005
  • 3
  • 35
  • 56
shex95
  • 1
  • 1
  • 1
    Hi, welcome to SO. Please consider reading up on [ask] and how to create a [reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It makes it easier for others to help you. – Heroka Nov 30 '15 at 16:36
  • 1
    Please, indent your code properly and check your brackets. It is very likely that they are wrong (although syntactically valid). – nicola Nov 30 '15 at 16:50
  • cheers guys, ive just realised ive done the daftest thing ever. output=matrix(0,ncol=2,nrow=b) should be inside the first curly bracket, not sure why i had it outside. – shex95 Nov 30 '15 at 16:55
  • I edited your question. See if the content of the function is the intended one. Please also provide values for `v` and `b` that trigger the error. – nicola Nov 30 '15 at 17:06

0 Answers0