我能';我在r中的for循环代码中找不到错误
我有以下代码:我能';我在r中的for循环代码中找不到错误,r,loops,R,Loops,我有以下代码: 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_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
}
test_x在第一个循环中i
仅取值2000,那么您应该修改第一个循环,如下所示:
for (i in 1:test_x) {
test_q[i] <- 40
}
for(1中的i:test_x){
test_q[i]您正在为(test_x中的i)
做,但是x_test=2000
所以您只为(2000中的i)做
所以i
将只取2000
,您必须为(i in 1:test_x)做
你完全正确。我不确定我怎么看不到。如果你将其作为解决方案发布,我会将其标记为已解决。