2011-06-26 48 views
2

作爲R程序的一部分,我創建了矩陣患者invitedmissinrow和標量numdropmaxses。這些都存在並被正確創建。R值的評估和OR語句

我再試試這條線

else if (invited[iPatient, iSession] >= maxses | 
    missinrow[iPatient,iSession] >= (numdrop+1)) { 
    patients[iPatient,iSession] <- 'D'} 

的部分|完美的作品前,|之後的部分似乎並沒有做任何事情。

我試着把這條線變成兩條語句,我試過||而不是|,我盯着它幾個小時。
幫助! 感謝

新增11年6月28日 程序從另一個文件中讀取參數,並創建數據。但這裏是一些輸出:

invited[1:5, 1:14] 

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] 
[1,] 0 1 2 3 4 5 6 7 8  9 10 11 12 12 
[2,] 0 1 2 3 4 5 6 7 8  9 10 11 12 12 
[3,] 0 1 2 3 4 5 6 7 8  9 10 11 12 12 
[4,] 0 0 0 1 2 3 4 5 6  7  8  9 10 11 
[5,] 0 0 0 1 2 3 4 5 6  7  8  9 10 11 

這是很好的

missinrow[1:5, 1:14] 

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] 
[1,] 1 2 3 4 5 6 7 8 9 10  0  0  0  0 
[2,] 1 2 3 4 5 6 7 8 9 10  0  0  0  0 
[3,] 1 2 3 4 5 0 0 0 0  0  0  1  0  0 
[4,] 0 0 1 2 0 0 1 2 3  4  5  6  7  0 
[5,] 0 0 1 2 3 4 5 6 7  8  9 10 11 12 

也很好,但

patients[1:5, 1:14] 

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] 
[1,] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "A" "S" "D" "D" 
[2,] "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "A" "A" "D" "D" 
[3,] "S" "S" "S" "S" "S" "A" "S" "A" "A" "A" "S" "S" "D" "D" 
[4,] "NA" "W" "S" "S" "A" "S" "S" "S" "S" "S" "S" "S" "S" "A" 
[5,] "NA" "W" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" "S" 

,我們看到,這裏invited高於maxses細胞(即12)都D,因爲他們應該的,但那些missinrownumdrop + 1高(這是4 +1)不是D

+1

我試着運行代碼,'missinrow [iPatient,iSession]'測試完全適合我。 「missinrow」可能沒有你期望的數據嗎? iPatient和iSession是正確的數字嗎? – Owen

+4

如果您創建一些樣本數據和預期結果並將其添加到您的問題中,它將有所幫助。 – Andrie

+2

嘗試在出現錯誤時檢查調試。你可以通過設置'options(error = recover)來實現' – nullglob

回答

1

下面是包括你的表達

invited <- 10 * matrix(12:1, nrow=3) 
missinrow <- matrix(1:12, nrow=3) 
patients <- matrix(rep("A",12), nrow=3) 
maxses <- 100 
numdrop <- 7 

for (iPatient in 1:3) { 
    for (iSession in 1:4) { 
     if (iPatient==iSession) { patients[iPatient,iSession] <- 'B'} 
     else if (invited[iPatient, iSession] >= maxses | 
      missinrow[iPatient,iSession] >= (numdrop+1)) { 
      patients[iPatient,iSession] <- 'D'} 
          } 
         } 

綜觀三個矩陣

> invited 
    [,1] [,2] [,3] [,4] 
[1,] 120 90 60 30 
[2,] 110 80 50 20 
[3,] 100 70 40 10 
> missinrow 
    [,1] [,2] [,3] [,4] 
[1,] 1 4 7 10 
[2,] 2 5 8 11 
[3,] 3 6 9 12 
> patients 
    [,1] [,2] [,3] [,4] 
[1,] "B" "A" "A" "D" 
[2,] "D" "B" "D" "D" 
[3,] "D" "A" "B" "D" 

我想說else if聲明如預期工作的一些代碼,並在該「d」 S patients的左側對應於第二個else if條件。