我想獲得一個表中的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")
這裏ou
是list(vldata$UIDALL)
@ Hack-R謝謝,我添加了子集數據 –
這是'vldata'的一個樣本,所以'ou'將會是'list(vldata $ UIDALL)'。我看到名字不一致。我糾正了這一點。 –
在代碼中有'OperatingUnit'。我認爲我們應該用'UIDALL'來代替它吧? –