【问题标题】:R Error: number of items to replace is not a multiple of replacement lengthR错误:要替换的项目数不是替换长度的倍数
【发布时间】:2015-10-23 15:53:46
【问题描述】:

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

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

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

和 BCstore 为:

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 包,这里使用的“size”函数是与 Matlab 对这个词的使用有关的函数。

提前感谢您的帮助。

【问题讨论】:

  • 请提供reproducible example,其中包含示例输入和定义的所有变量,以便我们了解实际情况。从错误中可以清楚地看出,您的一项作业涉及长度不兼容的向量,但由于我们不知道真正的尺寸是什么,因此无法确定是哪个。

标签: r loops matrix


【解决方案1】:

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

BCmth&lt;- data.frame()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多