2017-10-04 87 views
1

下面是我的代碼示例,其中每個時間步都依賴於前一個。如何簡化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   
+0

你可以使用'診斷可能想些辦法()'你的循環步驟中提取每次迭代的非零值,將其存儲在一個矢量,然後結合在一起。 – LAP

+0

不清楚,也許嘗試'x < - y < - z < - 1'來簡化代碼的某些部分?另外,閱讀*矢量化* – zx8754

+0

你可以發佈樣本輸出?你的問題不是很清楚 - 看起來你想要將數據重新整形爲長整型,所以你最後有G,MC,DC和NP列,對嗎? – shreyasgm

回答

1

我們使用rownames_to_column所以我們可以重建原始的訂單時,我們就完成了。

我們使用gather從寬到長的格式。在這種格式中,我們可以從列名稱中刪除數字,並按新列名稱(key)對數值進行總結。

然後我們只需要將其恢復爲原始格式。

df %>% 
    rownames_to_column %>% 
    gather(key = 'key', value = 'value', -rowname) %>% 
    mutate(key = stringr::str_extract(key, '[a-zA-Z]+')) %>% 
    group_by(rowname, key) %>% 
    summarise(value = sum(value)) %>% 
    ungroup %>% 
    spread(key = 'key', value = 'value') %>% 
    arrange(as.integer(rowname)) %>% 
    select(G, NP, MC, DC) 
# # A tibble: 17 x 4 
#  G NP MC DC 
# <int> <int> <int> <int> 
# 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  0 
# 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 
+0

謝謝@Paul。請原諒我的天真問題,但'%>%'是什麼意思?我只有三個星期的經驗,使用R. –

+0

@Francis_SU,'%>%'操作符來自'maggritr'包(用作'tidyverse'包的一部分),查看'tidyverse'小插件以查看它是如何使用的。 – shreyasgm

+0

謝謝@shreyasgm –