2016-12-29 207 views
0

我想獲得一個表中的for循環內計算出的所有值。我檢查了d6-d10每個長度的長度,它是35,與「ou」的長度相同。我能夠將每個這些值存儲爲向量。 「OU」,在這種情況下,像安哥拉,博茨瓦納等國名..表輸出 - For循環中R

ou<- sort(unique(vldata$OperatingUnit) 
for (i in ou) { 
d6 <- sum(vldata$FY2016APR[vldata$indicator=="TX_CURR" & vldata$OperatingUnit==i],na.rm = TRUE) 
d7 <- sum(vldata$FY2016Q2[vldata$indicator=="TX_CURR" & vldata$OperatingUnit==i],na.rm = TRUE) 
d8 <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$numeratorDenom=="D" & vldata$OperatingUnit==i], na.rm = TRUE) 
d9 <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$numeratorDenom=="N" & vldata$OperatingUnit==i], na.rm = TRUE) 
d10 <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$categoryOptionComboName=="Undetectable" & 
          vldata$OperatingUnit==i], na.rm = TRUE) 

dash1.table <- table(vldata$OperatingUnit,d6, d7, d8, d9, d10) 
} 

錯誤:Error in table(vldata$OperatingUnit, d6, d7, d8, d9, d10, : all arguments must have the same length

> str(ou) 
List of 1 
$ : Factor w/ 35 levels "Angola","Asia Regional Program",..: 1 1 1 1 1 1 1 1 1 1 ... 

str(vldata$OperatingUnit) 
Factor w/ 35 levels "Angola","Asia Regional Program",..: 1 1 1 1 1 1 1 1 1 1 ... 

運行循環後,我得到:

str(i) 
Factor w/ 35 levels "Angola","Asia Regional Program",..: 1 1 1 1 1 1 1 1 1 1 ... 

30樣本來自我的數據(> 200k行)的行。您可以使用UIDAll作爲操作單元名稱或國家/地區名稱。

structure(list(UIDAll = structure(c(4L, 19L, 30L, 21L, 12L, 1L, 
24L, 20L, 9L, 22L, 13L, 8L, 3L, 6L, 5L, 29L, 7L, 18L, 25L, 15L, 
2L, 23L, 11L, 14L, 17L, 16L, 10L, 27L, 26L, 28L), .Label = c("a7kj6yR3FRT", 
"a9b2mJ4Z5Kv", "bcYTjHaZcE4", "CVAHCZfm5MK", "d507u4dhfug", "dBBdk4e1xGM", 
"h54eHtqrMYc", "h5siIA7lQfx", "Hk6QKCvBV9B", "I3Pvb7ryppf", "IrB4tyQKF5E", 
"JgEjEEV5Nsy", "JKnoiKiTSgm", "loiaqD14rjG", "mtHydPyChpG", "nPh5JYQQyqa", 
"oWG1PMWA3N1", "QDkCAOGVng6", "QZwDp4lHXWp", "rV9MIblAWIF", "s6HYTxvqhzX", 
"sNi4VZ2HDoT", "WGkhTclMK4U", "wZsEzdJHKVJ", "XME74MPBxXl", "Y63LmT4BKqS", 
"ycP81nJei2m", "yhnSeJ5NaC2", "yqGgSdD5hP3", "Z4XPx6jaeFf"), class = "factor"), 
indicator = structure(c(2L, 2L, 1L, 3L, 3L, 3L, 3L, 2L, 3L, 
3L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 1L, 3L, 1L, 3L, 3L, 2L, 3L, 
2L, 3L, 2L, 2L, 3L, 2L), .Label = c("TX_CURR", "TX_UNDETECT", 
"TX_VIRAL"), class = "factor"), numeratorDenom = structure(c(2L, 
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("D", 
"N"), class = "factor"), categoryOptionComboName = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 5L, 3L, 1L, 5L, 1L, 1L, 1L, 1L, 5L, 1L, 
4L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 3L, 1L, 3L, 1L, 2L, 3L), .Label = c("default", 
"Detectable", "Routine", "Targeted", "Undetectable"), class = "factor"), 
FY2016Q2 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 764L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA), FY2016Q4 = c(71L, NA, NA, NA, 43L, NA, 
844L, 2130L, NA, 0L, -9L, 0L, NA, NA, 0L, NA, NA, 643L, NA, 
NA, NA, NA, 48L, NA, 889L, NA, 89L, 651L, NA, 115L), FY2016APR = c(71L, 
NA, NA, NA, 43L, NA, 844L, 2130L, NA, 0L, -9L, 0L, NA, NA, 
0L, NA, NA, 643L, NA, NA, NA, NA, 48L, NA, 889L, NA, 89L, 
651L, NA, 115L)), .Names = c("UIDAll", "indicator", "numeratorDenom", 
"categoryOptionComboName", "FY2016Q2", "FY2016Q4", "FY2016APR" 
), row.names = c(NA, -30L), class = "data.frame") 

這裏oulist(vldata$UIDALL)

+1

@ Hack-R謝謝,我添加了子集數據 –

+1

這是'vldata'的一個樣本,所以'ou'將會是'list(vldata $ UIDALL)'。我看到名字不一致。我糾正了這一點。 –

+0

在代碼中有'OperatingUnit'。我認爲我們應該用'UIDALL'來代替它吧? –

回答

0

它看起來像這個問題是你如何使用table()功能。

table()顯示輸入向量的每個級別的頻率。你給它多於一個等長的向量,然後它創建一個交叉表。但是,sum()的結果是標量。

您給table()的呼叫在每次迭代中都有一個向量與幾個標量混合在一起。這不起作用。

我想這是你想要的東西:

dash1.table <- data.frame(ou = ou, d6=NA, d7 = NA, d8=NA, d9=NA, d10=NA) 

for (i in ou) { 
    dash1.table$d6[dash1.table$ou==i] <- sum(vldata$FY2016APR[vldata$indicator=="TX_CURR" & vldata$UIDAll==i],na.rm = TRUE) 
    dash1.table$d7[dash1.table$ou==i] <- sum(vldata$FY2016Q2[vldata$indicator=="TX_CURR" & vldata$UIDAll==i],na.rm = TRUE) 
    dash1.table$d8[dash1.table$ou==i] <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$numeratorDenom=="D" & vldata$UIDAll==i], na.rm = TRUE) 
    dash1.table$d9[dash1.table$ou==i] <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$numeratorDenom=="N" & vldata$UIDAll==i], na.rm = TRUE) 
    dash1.table$d10[dash1.table$ou==i] <- sum(vldata$FY2016APR[vldata$indicator=="TX_VIRAL" & vldata$categoryOptionComboName=="Undetectable" & 
           vldata$UIDAll==i], na.rm = TRUE) 


} 

dash1.table 
  ou d6 d7 d8 d9 d10 
1 CVAHCZfm5MK 0 0 0 0 0 
2 QZwDp4lHXWp 0 0 0 0 0 
3 Z4XPx6jaeFf 0 0 0 0 0 
4 s6HYTxvqhzX 0 0 0 0 0 
5 JgEjEEV5Nsy 0 0 43 0 0 
6 a7kj6yR3FRT 0 0 0 0 0 
7 wZsEzdJHKVJ 0 0 0 844 844 
8 rV9MIblAWIF 0 0 0 0 0 
9 Hk6QKCvBV9B 0 0 0 0 0 
10 sNi4VZ2HDoT 0 0 0 0 0 
11 JKnoiKiTSgm 0 0 0 0 0 
12 h5siIA7lQfx 0 0 0 0 0 
13 bcYTjHaZcE4 0 0 0 0 0 
14 dBBdk4e1xGM 0 0 0 0 0 
15 d507u4dhfug 0 0 0 0 0 
16 yqGgSdD5hP3 0 0 0 0 0 
17 h54eHtqrMYc 0 0 0 0 0 
18 QDkCAOGVng6 643 764 0 0 0 
19 XME74MPBxXl 0 0 0 0 0 
20 mtHydPyChpG 0 0 0 0 0 
21 a9b2mJ4Z5Kv 0 0 0 0 0 
22 WGkhTclMK4U 0 0 0 0 0 
23 IrB4tyQKF5E 0 0 0 0 0 
24 loiaqD14rjG 0 0 0 0 0 
25 oWG1PMWA3N1 0 0 0 0 0 
26 nPh5JYQQyqa 0 0 0 0 0 
27 I3Pvb7ryppf 0 0 0 0 0 
28 ycP81nJei2m 0 0 0 0 0 
29 Y63LmT4BKqS 0 0 0 0 0 
30 yhnSeJ5NaC2 0 0 0 0 0 

注意,因爲這只是示例數據的樣本,有很多0。

+1

有道理!謝謝一噸,這工作。 –

+0

@anubhutimishra不客氣,高興地幫助 –

+0

哦,但現在它爲每個國家填寫完全相同的數據。任何想法爲什麼這可能是這種情況? –