下面是我的代碼示例,其中每個時間步都依賴於前一個。如何簡化for循環函數的輸出
my_func <- function(n=100, con=0.95, t=35, m=0.047){
G1<- numeric(length = t + 1)
G2<- numeric(length = t + 1)
G3<- numeric(length = t + 1)
G4<- numeric(length = t + 1)
G5<- numeric(length = t + 1)
G6<- numeric(length = t + 1)
G7<- numeric(length = t + 1)
G8<- numeric(length = t + 1)
G9<- numeric(length = t + 1)
MC1<- numeric(length = t + 1)
MC2<- numeric(length = t + 1)
MC3<- numeric(length = t + 1)
MC4<- numeric(length = t + 1)
MC5<- numeric(length = t + 1)
MC6<- numeric(length = t + 1)
DC <- numeric(length = t + 1)
NP <- numeric(length = t + 1)
G1[1]<- rbinom(1,n,con)
G2[1]<- 0
G3[1]<- 0
G4[1]<- 0
G5[1]<- 0
G6[1]<- 0
G7[1]<- 0
G8[1]<- 0
G9[1]<- 0
MC1[1]<- 0
MC2[1]<- 0
MC3[1]<- 0
MC4[1]<- 0
MC5[1]<- 0
MC6[1]<- 0
DC[1] <- 0
NP[1] <- 100 - G1[1]
for(step in 1:t){
G2[step+1] <- rbinom(1, G1[step], (1-m))
G3[step+1] <- rbinom(1, G2[step], (1-m))
G4[step+1] <- rbinom(1, G3[step], (1-m))
G5[step+1] <- rbinom(1, G4[step], (1-m))
G6[step+1] <- rbinom(1, G5[step], (1-m))
G7[step+1] <- rbinom(1, G6[step], (1-m))
G8[step+1] <- rbinom(1, G7[step], (1-m))
G9[step+1] <- rbinom(1, G8[step], (1-m))
MC1[step+1] <- rbinom(1, G9[step], (1-m))
MC2[step+1] <- rbinom(1, MC1[step], (1-m))
MC3[step+1] <- rbinom(1, MC2[step], (1-m))
MC4[step+1] <- rbinom(1, MC3[step], (1-m))
MC5[step+1] <- rbinom(1, MC4[step], (1-m))
MC6[step+1] <- rbinom(1, MC5[step], (1-m))
G1[step+1] <- rbinom(1, MC6[step],con)
NP[step+1] <- MC6[step]-G1[step+1]
DC[step+1] <- sum(G1[step]-G2[step+1],G2[step]-G3[step+1],G3[step]-
G4[step+1],G4[step]-G5[step+1],G5[step]-G6[step+1],G6[step]-
G7[step+1],G7[step]-G8[step+1],G9[step]-MC1[step+1],MC1[step]-MC2[step+1],MC2[step]-MC3[step+1],MC3[step]-MC4[step+1],MC4[step]-MC5[step+1],MC5[step]-MC6[step+1])
}
out <- cbind(G1, G2, G3, G4, G5, G6, G7, G8, G9, NP, MC1, MC2, MC3, MC4,
MC5, MC6, DC)
return(out)
}
my_func()
這將返回以下輸出:
G1 G2 G3 G4 G5 G6 G7 G8 G9 NP MC1 MC2 MC3 MC4 MC5 MC6 DC
[1,] 99 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
[2,] 0 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9
[3,] 0 0 87 0 0 0 0 0 0 0 0 0 0 0 0 0 3
[4,] 0 0 0 79 0 0 0 0 0 0 0 0 0 0 0 0 8
[5,] 0 0 0 0 77 0 0 0 0 0 0 0 0 0 0 0 2
[6,] 0 0 0 0 0 73 0 0 0 0 0 0 0 0 0 0 4
[7,] 0 0 0 0 0 0 68 0 0 0 0 0 0 0 0 0 5
[8,] 0 0 0 0 0 0 0 63 0 0 0 0 0 0 0 0 5
[9,] 0 0 0 0 0 0 0 0 61 0 0 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 0 53 0 0 0 0 0 8
[11,] 0 0 0 0 0 0 0 0 0 0 0 50 0 0 0 0 3
[12,] 0 0 0 0 0 0 0 0 0 0 0 0 47 0 0 0 3
[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 45 0 0 2
[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 0 1
[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 2
[16,] 41 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
[17,] 0 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
有一種方法,以該系列代碼,以便下G1
列生成的元素 - G9
將可以在單個列G
和觀察相應的元素在行[1,]
- [9,]
?這涉及當前代碼中其他變量的MC1
- MC6
等等。
在此先感謝。
UPDATE。
所需的輸出,我要找的是以下內容:
G NP MC DC
[1,] 99 1 0 0
[2,] 90 0 0 9
[3,] 87 0 0 3
[4,] 79 0 0 8
[5,] 77 0 0 2
[6,] 73 0 0 4
[7,] 68 0 0 5
[8,] 63 0 0 5
[9,] 61 0 0 2
[10,] 0 0 53 8
[11,] 0 0 50 3
[12,] 0 0 47 3
[13,] 0 0 45 2
[14,] 0 0 44 1
[15,] 0 0 42 2
[16,] 41 1 0 0
[17,] 40 0 0 1
你可以使用'診斷可能想些辦法()'你的循環步驟中提取每次迭代的非零值,將其存儲在一個矢量,然後結合在一起。 – LAP
不清楚,也許嘗試'x < - y < - z < - 1'來簡化代碼的某些部分?另外,閱讀*矢量化* – zx8754
你可以發佈樣本輸出?你的問題不是很清楚 - 看起來你想要將數據重新整形爲長整型,所以你最後有G,MC,DC和NP列,對嗎? – shreyasgm