简体   繁体   English

R:循环错误,替换长度为零

[英]R: Error in a Loop, replacement has length zero

I tried to find the solution looking at other questions but no one fits on my needs.我试图找到解决其他问题的解决方案,但没有一个适合我的需要。

I get the error in this reproducible example:我在这个可重现的例子中得到了错误:

m <- 0

vectorsd<-  rep(0.2, 183)

dgmean <- -0.41

  rnorm <- as.data.frame(rnorm(183, mean = m, sd = vectorsd))

for (i in 1:length(rnorm$`rnorm(183, mean = m, sd = vectorsd)`))  {
  rnorm[,i] <- rnorm(183, mean = m, sd = vectorsd)
}

simulation1 <- matrix(1,184,183)

beta <- -0.21

##For each column (each value of the forward curve)
for (i in 1:length(simulation1[1,])) {
  ##For each element of a whole column (each day since the starting of the product)
  for (j in 1:length(simulation1[,1])-1) {
    simulation1[j+1,i] <- simulation1[j,i]+rnorm[j,i]+(beta*(simulation1[j,i]-dgmean))
  }
}

I was expecting to get each row of simulation1 as from the second to be substituted with the transformation simulation1[j,i]+rnorm[j,i]+(beta*(simulation1[j,i]-dgmean)) but I get this error instead:我期望从第二行开始将每一行simulation1替换为转换simulation1[j,i]+rnorm[j,i]+(beta*(simulation1[j,i]-dgmean))但我得到这个错误:

Error in simulation1[j + 1, i] <- simulation1[j, i] + rnorm[j, i] + (beta *: replacement has length zero simulation1[j + 1, i] <- simulation1[j, i] + rnorm[j, i] + (beta *: 替换长度为零

What am I missing?我错过了什么? I am really burning my brain trying to find the typo/error我真的在努力寻找拼写错误/错误

This runs without any error:这运行没有任何错误:

m <- 0
vectorsd<-  rep(0.2, 183)
dgmean <- -0.41
rnorm <- as.data.frame(rnorm(183, mean = m, sd = vectorsd))

for (i in 1:length(rnorm$`rnorm(183, mean = m, sd = vectorsd)`))  {
   rnorm[,i] <- rnorm(183, mean = m, sd = vectorsd)
}

simulation1 <- matrix(1,184,183)
beta <- -0.21

##For each column (each value of the forward curve)
for (i in 1:length(simulation1[1,])) {
  ##For each element of a whole column (each day since the starting of the product)
   for (j in 1:(length(simulation1[,1]) - 1)) {
     simulation1[i,j] <- simulation1[i,j]+rnorm[i,j]+(beta*(simulation1[i,j]-dgmean))
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM