假設我正在使用以下固定引數:
#Fixed Parameters
Start<-7200
P1_Input<-10000 #input from current period
P2_Input<-10000 #input from current 1 (next) period
Notional<-3.4*2000
Lot<-round((Start P1_Input P2_Input)/Notional,0)
Lot_Unit<-Lot*0.13*2000
Lot_Unit
End_Bal<-Start Lot_Unit
如果我想將 n=1 處的 End_Bal 的值設定為 n=2 處的下一個起始值,保持所有引數相同,最終目標是找到 P1_Input 和 P2_Input 的總和,以及 End_Bal 值n=20(或 n 的任何亂數,可以是 20、40、60 等),我需要在這里寫一個回圈嗎?非常感謝您的幫助。
uj5u.com熱心網友回復:
由于矢量化,回圈在 R 中不像在其他編程語言中那樣經常使用,但它們仍然有其用途。反饋回圈是非自然矢量化的一個很好的例子。在這種情況下,for 或 while 回圈很自然:
Start <- 7200
for(i in 1:10){
P1_Input <- 10000 #input from current period
P2_Input <- 10000 #input from current 1 (next) period
Notional <- 3.4 * 2000
Lot <- round((Start P1_Input P2_Input) / Notional, 0)
Lot_Unit <- Lot * 0.13 * 2000
Lot_Unit
End_Bal <- Start Lot_Unit
Start <- End_Bal #for next pass through loop
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519804.html
標籤:r循环
上一篇:回圈創建多個串列,具有不同的命名
下一篇:大學問題,包括for回圈和串列
