R 顺序列和
我的数据框架如下:R 顺序列和,r,R,我的数据框架如下: init = 0 final = 8000 out = 3000 vals = c(rexp(10000, 0.0002)) 我想在上面的列表中添加具有变量线性组合的列(30),如下所示: col1 = init + vals(1,) - out col2 = col1 + vals(1,) - out ... col30 = col29 + vals(1,) - out 并对所有10000行执行此操作: col1 = init + vals(
init = 0
final = 8000
out = 3000
vals = c(rexp(10000, 0.0002))
我想在上面的列表中添加具有变量线性组合的列(30),如下所示:
col1 = init + vals(1,) - out
col2 = col1 + vals(1,) - out
...
col30 = col29 + vals(1,) - out
并对所有10000行执行此操作:
col1 = init + vals(n,) - out
col2 = col1 + vals(n,) - out
...
col30 = col29 + vals(n,) - out
我如何使用循环来实现这一点
尽管如此,每个计算值不能大于“final”且不小于“init”,并且应该四舍五入到这些值(8000和0)。您可以使用简单的for循环对30列执行此循环。对于10000行,您不需要循环,因为它们是使用向量自动计算的pmin
和pmax
用于每个计算值不能大于“final”且不小于“init”的条件cbind
将列连接在一起
init = 0
final = 8000
out = 3000
vals = c(rexp(10000, 0.0002))
tt <- pmin(final, pmax(init, init + vals - out))
vals <- cbind(vals, tt)
for(i in 2:30) {
tt <- pmin(final, pmax(init, tt + vals[,1] - out))
vals <- cbind(vals, tt)
}
dimnames(vals)[[2]][2:31] <- paste0("col", 1:30)
init=0
最终=8000
out=3000
VAL=c(雷克斯普(10000,0.0002))
tt