我有以下數據框:的R - 基於多種其他因素的列求和列
df<-structure(list(totprivland = c(175L, 50L, 100L, 14L, 4L, 240L,
10L, 20L, 20L, 58L), ncushr8d1 = c(0L, 0L, 0L, 0L, 0L, 30L, 5L,
0L, 0L, 50L), ncu_CENREG1 = structure(c(4L, 4L, 4L, 4L, 1L, 3L,
3L, 3L, 4L, 4L), .Label = c("Northeast", "Midwest", "South",
"West"), class = "factor"), ncushr8d2 = c(75L, 50L, 100L, 14L,
2L, 30L, 5L, 20L, 20L, 8L), ncu_CENREG2 = structure(c(4L, 4L,
4L, 4L, 1L, 2L, 1L, 4L, 3L, 4L), .Label = c("Northeast", "Midwest",
"South", "West"), class = "factor"), ncushr8d3 = c(100L, NA,
NA, NA, 2L, 180L, 0L, NA, NA, NA), ncu_CENREG3 = structure(c(4L,
NA, NA, NA, 1L, 1L, 3L, NA, NA, NA), .Label = c("Northeast",
"Midwest", "South", "West"), class = "factor"), ncushr8d4 = c(NA,
NA, NA, NA, 0L, NA, NA, NA, NA, NA), ncu_CENREG4 = structure(c(NA,
NA, NA, NA, 1L, NA, NA, NA, NA, NA), .Label = c("Northeast",
"Midwest", "South", "West"), class = "factor")), .Names = c("totprivland",
"ncushr8d1", "ncu_CENREG1", "ncushr8d2", "ncu_CENREG2", "ncushr8d3",
"ncu_CENREG3", "ncushr8d4", "ncu_CENREG4"), row.names = c(27404L,
27525L, 27576L, 27822L, 28099L, 28238L, 28306L, 28312L, 28348L,
28379L), class = "data.frame")
=======
這是dput
以下基本思路:
Total VariableA LocationA VariableB LocationB
30 20 East 10 East
20 20 South NA West
115 15 East 100 South
100 50 West 50 West
35 10 East 25 South
總數(或dput示例中的totprivland)是變量(ncushr8d1,ncushr8d2,ncushr8d3和ncushr8d4)的總和,並且每個變量都有相應的因子位置變量(ncu_CENRE G1等)。在這種模式下還有6個額外的變量和位置。位置變量對於多個數值變量通常是相同的值(例如,多個'東'位置值,如示例的第一行)。
我想通過公共位置因子得到每行值的總和,爲每個位置的總和創建一個新列。這將是這個樣子,與忽略NA值的能力:
Total VariableA LocationA VariableB LocationB TotalWest TotalEast TotalSouth
30 20 East 10 East 0 30 0
20 20 South NA NA 0 0 20
115 15 East 100 South 0 15 100
100 50 West 50 West 100 0 0
35 10 East 25 South 0 10 25
我看着聚集和分裂,但似乎無法弄清楚如何讓他們的工作遇到了許多列。我還在考慮一個冗長的「if」語句,它將在所有8個變量及其相應的位置上旋轉,但感覺必須有更好的解決方案。這些觀察結果被加權用於調查軟件包中,我想避免重複觀察結果,並使用重塑軟件包使它們「長」,儘管也許我可以在以後重新組合它們。任何建議感激!
許多感謝, 盧克
你的榜樣輸入的第一行是沒有意義對我來說,'TotalWest'列有20個值,但LocationA和LocationB既沒有西方。另外,您是否可以使您的示例數據集更容易進入-R可供選擇的形式(例如使用'dput')。 –
感謝Paul,我糾正了TotalWest專欄。我從來沒有使用dput,但會嘗試做到這一點... –