double for循环,使用R存储每个值

时间:2014-11-20 05:54:30

标签: r for-loop matrix

我想使用double for循环来设置一个包含6列和2 ^ 6 = 64行的矩阵。对于每一行,我试图将每个值从S1存储到S6。然后找出它们的平均值。之后,为所有64行运行out循环。我可以用非常复杂的方式做到如下。

w <- expand.grid(rep(list(0:1),6))
for(i in 1:dim(w)[1])
{
  S0<-20
  u <- 1.1
  d <- .92
  S1 <- S0*u^w[i,1]*d^(1-w[i,1])
  S2 <- S0*u^w[i,2]*d^(1-w[i,2])
  S3 <- S0*u^w[i,3]*d^(1-w[i,3])
  S4 <- S0*u^w[i,4]*d^(1-w[i,4])
  S5 <- S0*u^w[i,5]*d^(1-w[i,5])
  S6 <- S0*u^w[i,6]*d^(1-w[i,6])
  a <- c(S1,S2,S3,S4,S5,S6)
  mean <- (S0+S1+S2+S3+S4+S5+S6)/7
}

任何人都可以帮助我完成内循环。我在想的是使用内部for循环,例如:

for(j in 1:dim(w)[2])  
{
  S <- S0*u^w[i,j]*d^(1-w[i,j])
  sum <- S0+S
  mean <- sum/7
}

这绝对是错误的,因为S只被下一个值替换,只存储在终端中。即使我有S1到S6用完了,我也没有存储它。如果这些值可以存储在矩阵中,那将是很好的。谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用apply非常轻松地在每行上执行一项功能:

S0 <- 20
u <- 1.1
d <- .92

means <- apply(w, 1, function (x) {
  sum(S0,S0*u^x * d^(1-x))/7
})
means
# [1] 18.62857 19.14286 19.14286 19.65714 19.14286 19.65714 19.65714 20.17143 19.14286 19.65714 19.65714 20.17143
# [13] 19.65714 20.17143 20.17143 20.68571 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
# [25] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000 19.14286 19.65714 19.65714 20.17143
# [37] 19.65714 20.17143 20.17143 20.68571 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
# [49] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000 20.17143 20.68571 20.68571 21.20000
# [61] 20.68571 21.20000 21.20000 21.71429

但是如果你想坚持循环,那么你需要指定一个对象来存储输出OUTSIDE循环,然后在向量的相应位置分配值。同样如@nicola所提到的,鉴于R中的操作是矢量化的,你不需要第二个循环:

means <- vector(length=dim(w)[1])

for(i in 1:dim(w)[1]) {
    S <- S0*u^w[i,]*d^(1-w[i,])
    means2[i] <- (sum(S)+S0)/7
}
means2
# [1] 18.62857 19.14286 19.14286 19.65714 19.14286 19.65714 19.65714 20.17143 19.14286 19.65714 19.65714 20.17143
# [13] 19.65714 20.17143 20.17143 20.68571 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
# [25] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000 19.14286 19.65714 19.65714 20.17143
# [37] 19.65714 20.17143 20.17143 20.68571 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
# [49] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000 20.17143 20.68571 20.68571 21.20000
# [61] 20.68571 21.20000 21.20000 21.71429

答案 1 :(得分:0)

或者@nicola在评论中建议,这可以在没有任何循环的情况下完成

a<- S0*u^w*d^(1-w)
rowMeans(cbind(S0, a))
#[1] 18.62857 19.14286 19.14286 19.65714 19.14286 19.65714 19.65714 20.17143
#[9] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
#[17] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
#[25] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
#[33] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
#[41] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
#[49] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
#[57] 20.17143 20.68571 20.68571 21.20000 20.68571 21.20000 21.20000 21.71429

res <- (rowSums(a)+S0)/7 
res
# [1] 18.62857 19.14286 19.14286 19.65714 19.14286 19.65714 19.65714 20.17143
# [9] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
# [17] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
# [25] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
# [33] 19.14286 19.65714 19.65714 20.17143 19.65714 20.17143 20.17143 20.68571
# [41] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
# [49] 19.65714 20.17143 20.17143 20.68571 20.17143 20.68571 20.68571 21.20000
# [57] 20.17143 20.68571 20.68571 21.20000 20.68571 21.20000 21.20000 21.71429

数据

w <- expand.grid(rep(list(0:1),6))
S0 <- 20
u <- 1.1
d <- .92