Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在迭代过程中使用最终值_R_Matrix_Sapply_Cbind - Fatal编程技术网

R 在迭代过程中使用最终值

R 在迭代过程中使用最终值,r,matrix,sapply,cbind,R,Matrix,Sapply,Cbind,以下是我正在使用的代码: ## Round 1 b <- c(5000,2500) r <- c(2,2.1) pm <- c(200,240) I <- b*(r/1200) pi <- pm-I end <- b-pi end ## Round 2 b1 <- end r1 <- (2.3,2.3) I1 <- b1*(r/1200) pi1 <- pm-I1 end1 <- b1-pi1 end1 这

以下是我正在使用的代码:

## Round 1
b <- c(5000,2500)
r <- c(2,2.1) 
pm <- c(200,240)

I <- b*(r/1200) 
pi <- pm-I
end <- b-pi  
end

## Round 2 
b1 <- end 
r1 <- (2.3,2.3) 
I1 <- b1*(r/1200)
pi1 <- pm-I1 
end1 <- b1-pi1 
end1
这没有产生所需的输出,即:

[1,] 4808.333 4616.347
[2,] 2264.375 2028.338
因为“end”变量需要更新的“b”值,如下面代码中带星号的部分所示,所以我应该怎么做呢

    **b1 <- end** 
    r1 <- 2.3 
    I1 <- b1*(r/1200)
    pi1 <- pm-I1 
    end1 <- b1-pi1 
    end1

**b1这是一种for循环发光的情况:

rlist <- list(r, r1)
x <- vector("list", 2)
for(i in 1:2) {b <- b - (pm - b*(rlist[[i]]/1200)); x[[i]] <- b}
`names<-`(as.data.frame(x), c('x', 'y'))
#         x        y
#1 4808.333 4617.549
#2 2264.375 2028.715

rlist感谢您的快速响应,我总是被告知要远离R中的for循环。rlist函数是特殊包的一部分吗?我很抱歉。请参见编辑。我创建了一个“r”值列表
sapply
for
循环之间并没有区别。@davidernburg我也一直这么认为
    **b1 <- end** 
    r1 <- 2.3 
    I1 <- b1*(r/1200)
    pi1 <- pm-I1 
    end1 <- b1-pi1 
    end1
rate <- rbind(r,r1) 
t(sapply(b, function(x) {b <- b - (pm - b*(rate/1200));b}))
rlist <- list(r, r1)
x <- vector("list", 2)
for(i in 1:2) {b <- b - (pm - b*(rlist[[i]]/1200)); x[[i]] <- b}
`names<-`(as.data.frame(x), c('x', 'y'))
#         x        y
#1 4808.333 4617.549
#2 2264.375 2028.715