R中的循环问题,返回NA

时间:2018-02-18 15:16:47

标签: r

fact.weight <- matrix(NA,nrow(dax.p),2)

fact.ret <- matrix(NA,nrow(dax.p),2)  
fact.sd <- matrix(NA,nrow(dax.p),2)  
fact.cov <- matrix(NA,ncol(fact.weight),ncol(fact.weight))

for (row in 15:nrow(fact.weight)) {
   for (column in 1:ncol(fact.weight)) {
     fact.ret[row,column] <- mean(fact.wealth.return[row-1:row,column])  
     fact.sd[row,column] <- sd(fact.wealth.return[row-1:row,column])
  }
}

我的循环有问题。 fact.retfact.sd仅返回NA个值。问题可能出在某处

mean(fact.wealth.return[row-1:row,column])
sd(fact.wealth.return[row-1:row,column])

但我自己也看不到问题。有人看到这个问题吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题似乎出现在你的矢量子集中,我想你想要:

mean(fact.wealth.return[(row-1):row,column])
sd(fact.wealth.return[(row-1):row,column])

要了解原因,请尝试:

row = 10    
row-1:row

[1] 9 8 7 6 5 4 3 2 1 0

并将其与

进行比较
row = 10
(row-1):row

[1]  9 10