2017-02-28 64 views
-1

我想彙總這個數據框,其中每個Family Size有六個類別,Hours Worked。所有的多個條件下的聚合列

families <- structure(list(`Family Size` = c(2L, 2L, 2L, 2L, 2L, 2L, 2L,13L, 13L, 13L), HoursLess20 = c("1,014", "1,041", "11", "3","1", "2", "1", "0", "0", "0"), Hours2024 = c(7L, 298L, 1L, 0L,0L, 0L, 0L, 0L, 0L, 0L), Hours2529 = c(1L, 34L, 0L, 0L, 0L, 0L,0L, 0L, 0L, 0L), Hours3034 = c(6L, 44L, 1L, 0L, 0L, 0L, 0L, 0L,0L, 0L), Hours3539 = c(4L, 46L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Hours40plus = c(9L, 128L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Family Size","HoursLess20", "Hours2024", "Hours2529", "Hours3034", "Hours3539","Hours40plus"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1977L,1978L, 1979L), class = "data.frame") 
+0

你想如何聚合?總和?平均? – G5W

回答

1

首先,您目前擁有的值HoursLess20爲字符串 (因爲逗號)。要進行任何類型的數值聚合,您需要刪除逗號並將其轉換爲數字。

families$HoursLess20 = as.numeric(gsub(",", "", families$HoursLess20)) 

一旦你這樣做,你可以只使用聚合函數 創造任何你想要的集合體。

## Sum 
aggregate(families[,-1], list(families[,1]), sum) 
    Group.1 HoursLess20 Hours2024 Hours2529 Hours3034 Hours3539 Hours40plus 
1  2  2073  306  35  51  50   138 
2  13   0   0   0   0   0   0 

## Average 
aggregate(families[,-1], list(families[,1]), mean) 
    Group.1 HoursLess20 Hours2024 Hours2529 Hours3034 Hours3539 Hours40plus 
1  2 296.1429 43.71429   5 7.285714 7.142857 19.71429 
2  13  0.0000 0.00000   0 0.000000 0.000000  0.00000 
+0

我不知道爲什麼會發生這種情況,但是當我將它複製爲SO時肯定發生了,但謝謝。 –