我認爲我的問題對於普通的R用戶來說非常簡單。在R中創建一個循環變量
編輯: 我的例子可能是我的真正問題的簡化版本: 請看下面的代碼:
library(markovchain)
#create a new MC with transition matrix transitionMatrix
mcStaticPool <- new("markovchain", states=c("perf", "def", "prep"),
transitionMatrix=matrix(data=c(0.89715, 0.01475, 0.08810, 0, 1, 0, 0, 0, 1),
byrow=TRUE, nrow=3), name="StaticPool")
#display transition matrix
#print(mcStaticPool)
show(mcStaticPool)
#initially all loans are performing
initialState<-c(1,0,0)
nYears<-10
for(i in 1:nYears){
afterNYears<-initialState*(mcStaticPool^i)
print(afterNYears)
}
我想這在載體中。實際上只有數字。而不是國家。
perf def prep
[1,] 0.89715 0.01475 0.0881
perf def prep
[1,] 0.8048781 0.02798296 0.1671389
perf def prep
[1,] 0.7220964 0.03985491 0.2380487
perf def prep
[1,] 0.6478288 0.05050584 0.3016654
perf def prep
[1,] 0.5811996 0.06006131 0.3587391
perf def prep
[1,] 0.5214232 0.06863401 0.4099428
perf def prep
[1,] 0.4677948 0.076325 0.4558802
perf def prep
[1,] 0.4196821 0.08322497 0.4970929
perf def prep
[1,] 0.3765178 0.08941528 0.5340669
perf def prep
[1,] 0.337793 0.09496892 0.5672381
以下循環:
for(i in 1:10){
test<-2*(2^i)
print(test)
}
這給:
[1] 4
[1] 8
[1] 16
[1] 32
[1] 64
[1] 128
[1] 256
[1] 512
[1] 1024
[1] 2048
我想這個保存爲一個變量,例如叫test2的。
我嘗試這樣做:
for(i in 1:10){
test<-2*(2^i)
test2 <- print(test)
}
但隨後給出:
> test2
[1] 2048
但我想在test2的整個序列:
[1] 4
[1] 8
[1] 16
[1] 32
[1] 64
[1] 128
[1] 256
[1] 512
[1] 1024
[1] 2048
感謝, 最好的問候,
添
你不能都在一個單一的載體。你的意思是數據框?也許三個向量,每個'perf','def','prep'? –
也許我不清楚,我只是想把百分比作爲一個向量。應該看起來像這樣:c(0.0881,0.89715,0.01475,0.1671389,0.8048781,0.02798296,0.22380487,0.7220964,0.03985491,0.3016654,0.66478288,0.05050584,0.3587391,0.5811996,0.06006131,0.4099428,0.5214232,0.06863401,0.4558802,0.4677948,0.076325,0.4970929 ,0.4196821,0.08322497,0.5340669,0.3765178,0.08941528,0.5672381,0.3337793,0.09496892) –