我有这个代码:
test_x <- 2000
#functions used:
`%+<-%` = function(e1,e2) eval.parent(substitute(e1 <- e1 + e2))
CreateData <- function()
{
numeric(test_x)
}
#variables
test_sum <- 0
test_q <- 0
test_q <- CreateData()
test_Q <- 0
test_Q <- CreateData()
test_step <- 0.01
#two for loops
for (i in test_x) {
test_q[i] <- 40
}
for (i in (test_x - 1):0) {
test_sum %+<-% (test_q[i]*test_step)
test_Q[i] <- test_sum
}
我期望第一个 for 循环会在 2000 个位置中的每个位置用 40 填充 test_q,但每个位置都为零。因此,第二个循环也保持为零。
没有错误。我做错了什么?
在第一个循环i
中只取值 2000,那么您应该按如下方式修改第一个循环:
for (i in 1:test_x) {
test_q[i] <- 40
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句