2013-06-06 2 views
0

에서 집계 한 후 내가 먼저 다른 열 당 데이터 프레임에 하나 개의 컬럼의 평균을 얻기 위해 aggregate을 사용 :정렬 R

meanDemVoteHouseState <- aggregate(congress$X2012.House.Dem.vote, 
          by = list(state = congress$state), 
            FUN = mean) 

그때 위해이를 인쇄하고 싶었다. 우선 새로운 데이터 프레임

str(meanDemVoteHouseState) 

보았다 분명히, 새로운 변수가 이제 "X"라고

'data.frame': 50 obs. of 2 variables: 
$ state: chr "AK" "AL" "AR" "AZ" ... 
$ x : num 0.29 0.34 0.29 0.462 0.566 ... 

을 얻었다.

내가 그에 정렬 시도 때 :

meanDemVoteHouseState[order(x),] 

내가 오류 "을 (를) 찾을 수 없습니다 'X'객체"를 얻었다.

다른 많은 것들을 시도했지만 아무런 효과가 없었습니다.

무엇이 누락 되었습니까? 당신이 두 단계에서 그것을 할 경우, 당신은

meanDemVoteHouseState[order(meanDemVoteHouseState[,"x"]),] 

을 원하는

+0

의회 데이터 샘플을 제공 할 수 있습니까? – storaged

+2

'meanDemVoteHouseState [with (meanDemVoteHouseState, order (x))]]'또는'meanDemVoteHouseState [order (meanDemVoteHouseState $ x)]]' – Arun

답변

3

명확하게

myind <- order(meanDemVoteHouseState[,"x"]) # need 'x' fully qualified 
meanDemVoteHouseState[myind, ] 

또는 with() 같은 것들을 사용하여 ...

+0

그게 다야! 감사합니다 –

2

아마 할 쉬울 것

meanDemVoteHouseState <- aggregate(X2012.House.Dem.vote ~ state, 
            data = congress, FUN = mean) 

변수 이름을 유지 보수하는 이름.

ord <- with(meanDemVoteHouseState, order(X2012.House.Dem.vote)) 
meanDemVoteHouseState <- meanDemVoteHouseState[ord, ] 

이 시점에서 변수와 개체에 대해 더 짧은 이름을 선택하는 것이 좋습니다.

+0

감사. 그것은 또한 작동 할 것입니다. 변수 이름을 좀 더 합리적인 것으로 변경하는 바람입니다. –