2011-01-20 3 views
9

R에서 aggregate 함수에 대한 도움말 페이지를 보았습니다.이 편리한 함수를 사용하지는 못했지만 처리 속도가 빨라졌습니다. 그러나 나는 예제를 통해 진행되고있는 일을 완전히 이해할 수 없었습니다.Aggregate() 예제에서 R

1> aggregate(state.x77, list(Region = state.region), mean) 
     Region Population Income Illiteracy Life Exp Murder HS Grad Frost Area 
1  Northeast  5495 4570  1.000 71.26 4.722 53.97 132.78 18141 
2   South  4208 4012  1.738 69.71 10.581 44.34 64.62 54605 
3 North Central  4803 4611  0.700 71.77 5.275 54.52 138.83 62652 
4   West  2915 4703  1.023 71.23 7.215 62.00 102.15 134463 

여기에 출력 내가 기대 정확히 :

한 예는 다음과 같다. 그래서 나는 무슨 일이 일어나고 있는지 이해하려고 노력합니다. 그래서 보니 state.x77

1> head(state.x77) 
      Population Income Illiteracy Life Exp Murder HS Grad Frost Area 
Alabama   3615 3624  2.1 69.05 15.1 41.3 20 50708 
Alaska   365 6315  1.5 69.31 11.3 66.7 152 566432 
Arizona   2212 4530  1.8 70.55 7.8 58.1 15 113417 
Arkansas   2110 3378  1.9 70.66 10.1 39.9 65 51945 
California  21198 5114  1.1 71.71 10.3 62.6 20 156361 
Colorado   2541 4884  0.7 72.06 6.8 63.9 166 103766 

OK, 저도 이상합니다. state.x77에 state.region 또는 뭔가라는 열이있을 것으로 예상됩니다. 따라서 state.region은 자체 개체 여야합니다. 그래서 str()을 수행합니다 :

1> str(state.region) 
Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ... 

state.region은 하나의 요소입니다. 어떻게 든 state.region과 state.x77 사이의 연결이 있어야 aggregate()가 state.region에 의해 state.x77을 그룹화 할 수 있습니다. 하지만 그 연관성은 나에게 수수께끼입니다. 내 오해를 채울 수있게 도와 주시겠습니까?

답변

10

오래된 탐에서 폰 (그것은 탐폰 이었습니까?) 상업용 : "증거, 약속뿐입니다!"

state.x777 <- as.data.frame(state.x77) 
state.x777 <- cbind(state.x777, stejt.ridzn = state.region) 
aggregate(state.x77, list(Region = state.x777$stejt.ridzn), mean) 
+2

+1! –

4

그들은이 이러한 개체가 동일한 도움말 페이지 ?state.x77에 문서화 된대로 올바른 순서에 가능성 :

Details: 

    R currently contains the following “state” data sets. Note that 
    all data are arranged according to alphabetical order of the state 
    names. 
+2

Doh! 데이터에 도움말 페이지가 있다는 것을 잊었습니다. 유효성 확인을 위해 –

1

help(state.region)가 --- 그들은 모든 정렬 등을보십시오 :

을 세부 :

R currently contains the following “state” data sets. Note that 
all data are arranged according to alphabetical order of the state 
names. 
관련 문제