0

Good afternoon. I am working in R (in RStudio specifically) and have an error which I have seen in other threads but have been unable to translate the solutions to my own code.

I have quite a lengthy "if" loop (with nested "else" and "for" statements), however the key parts of my code are that the variable BCmth is defined as:

BCmth<- matrix(NA, nrow = 3000000,ncol = 4)

and BCstore as:

BCstore<- matrix(1,1000,2*round(iForeper))

And in this section of the loop they are used as such:

BCmth[,4] <- cumsum(BCmth[,3]) # compute cumulative density booking curve
BCstore[1:size(BCmth[,4],1),m] <- BCmth[,2] # store the month for the booking curve
BCstore[1:size(BCmth[,4],1),m+1] <- BCmth[,4] / BCmth[length(BCmth):4]  # store the booking curve  value as a %-age 
BCmref[1,m] <- BCmth[1,1] 
m <- m+2
} 

And I am receiving the below error after a run of the entire loop:

Error in BCstore[1:size(BCmth[, 4], 1), m] <- BCmth[, 2] : 
  number of items to replace is not a multiple of replacement length

PLEASE NOTE: I am using the matlab package and the "size" function used here is the function relating to Matlab's usage of the word.

Thank you in advance for any help.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Qaribbean
  • 178
  • 2
  • 3
  • 17
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and all the variables defined so we can see what's really happening. It seems pretty clear from the error that one of your assignments involves vectors of incompatible length but since we have no idea what the true dimensions are, it's impossible to say which with any certainty. – MrFlick Oct 23 '15 at 16:06

1 Answers1

0

In searching for a solution I have solved my problem by defining BCmth as a blank matrix (which was originally required in my code specification):

BCmth<- data.frame()

Qaribbean
  • 178
  • 2
  • 3
  • 17