1

I have coding of a function in R as following:

  matching_score=function(nitems, tot.score) {

  nInterval <- 4*nitems+1
  tot <- array(0, dim=c(nInterval,2,nGroup.all) )
  minimum <- nitems
  maximum <- nitems*5
  tot[,1,] <- c(minimum: maximum)    
    for (nGcut in 1:nGroup.all)
    {

... But R gave an error message as : Error in tot[, 1, ] <- c(minimum:maximum) : incorrect number of subscripts How can I solve this issue? When minimum and maximum were actual numbers, the error was not presented.

Thanks in advance for your advice.

lucyh
  • 179
  • 2
  • 5
  • 14
  • 4
    I recommend creating a complete function (seems that the 'for' loop is not needed for your question) and including a call, ending up with a reproducible example. – Matthew Lundberg Jun 16 '12 at 20:36
  • 1
    You still need to actually call the function for this to be a reproducible example. And what's nGroup.all? Also, as I mentioned above, as the error occurs just before the 'for' loop, you (probably) do not need anything after that in the function. – Matthew Lundberg Jun 16 '12 at 21:16
  • 2
    Have a look here for help on how to create a [great reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – betabandido Jun 16 '12 at 21:31
  • Thanks for your advice. I found the error occurred because of nGroup.all, which should have specified as integer rather than a vector. – lucyh Jun 18 '12 at 13:29
  • Also, the great reproducible example in R is helpful. Thanks. – lucyh Jun 18 '12 at 13:30

1 Answers1

0

The error probably occurs when you try to cbind the tot object. The error message is complaining about dimensions. You are using "[" as if this object is an array with three dimensions and 'cbind' will not work with arrays. If it's really a three dimensional object the install package 'abind' and use function abind.

require(abind)
arr <- array(1:(2*3*4), c(4,3,2) )
abind(arr, arr[,,1], along=3)

The dimensions of this line:

tempo[nRw,] <- cbind(tot[nRw,1,1], sum(tot[nRw,2,]))

... seem all wrong. The LHS has two dimensions, the 'tot' object has three and the return from sum will be a scalar.

IRTFM
  • 258,963
  • 21
  • 364
  • 487