R错误:要替换的项目数不是替换长度的倍数

时间:2015-10-23 15:53:46

标签: r loops matrix

下午好。我在R(特别是在RStudio)工作并且有一个错误,我在其他线程中看到但是无法将解决方案转换为我自己的代码。

我有一个很长的“if”循环(使用嵌套的“else”和“for”语句),但是我的代码的关键部分是变量BCmth定义为:

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

和BCstore as:

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

在循环的这一部分中,它们被用作:

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
} 

我在整个循环运行后收到以下错误:

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

请注意:我使用的是matlab软件包,这里使用的“大小”函数是与Matlab使用该单词相关的函数。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在搜索解决方案时,我通过将BCmth定义为空白矩阵(我的代码规范中最初需要)解决了我的问题:

BCmth<- data.frame()