2012-08-30 4 views
-5

가능한 중복 COL1 그룹의 평균을 찾는 방법 : 나는 R.에서 다른 열을 사용하여 컬럼의 평균을 찾기 위해 노력하고
Idiomatic R code for partitioning a vector by an index and performing an operation on that partition
How to calculate median of profits for a particular countryCOL2에 의해

을 SQL에서는 그렇게하기는 쉽지만 적절한 함수를 찾아 구현할 수는 없습니다. 아래에 샘플 데이터가 있습니다. 닮은 dat라는 data.frame 함께

data("Forbes2000", package = "HSAUR") 
head(Forbes2000) 

## rank    name  country    category sales profits assets marketvalue 
## 1 1   Citigroup United States    Banking 94.71 17.85 1264.03  255.30 
## 2 2 General Electric United States  Conglomerates 134.19 15.59 626.93  328.54 
## 3 3 American Intl Group United States   Insurance 76.66 6.46 647.66  194.87 
## 4 4   ExxonMobil United States Oil & gas operations 222.88 20.96 166.99  277.02 
## 5 5     BP United Kingdom Oil & gas operations 232.57 10.27 177.57  173.54 
## 6 6  Bank of America United States    Banking 49.01 10.81 736.45  117.55 
+2

여기에는 여러 가지 방법이 있습니다. 'plyr','data.table','sqldf' 꾸러미 (몇가지 언급)와 밑줄 (base) R에서'aggregate','tapply'와'ave' 함수를보십시오. 너무 많은 관련 질문이 있습니다. – mnel

+2

다시 검색해 보셔야합니다.이 질문은 100 번 답변되었습니다. 'plyr','data.table' 태그뿐만 아니라 특히'ddply' 함수,''aggregate'','ave' 또는 다른 수의 함수를 확인하십시오. [이 질문] (http://stackoverflow.com/questions/10748253/idiomatic-r-code-for-partitioning-a-vector-by-an-index-and-performing-operoper/10/748470#10748470O) 대부분의 선택 사항에 대한 합리적인 범위의 목록과 각각에 대한 상대적 시간표. – Chase

+1

이 질문에 대한 부정적인면의 일부는 아마도 R의 일반적인 read. * 함수로 그 파일을 가져올 수 없기 때문일 것입니다. utils 패키지에서'read.fwf'로 할 수는 있지만 고통 스럽습니다. 데이터 프레임의 머리 부분에 적용된'dput()'의 출력을 게시하는 방법을 배웁니다. –

답변

2

:

 rank    name country  category sales profits assets marketvalue 
21  21 DaimlerChrysler Germany Consumer_dur 157.13 5.12 195.58  47.43 

시도 (안된 D/t 그것의 의미를 만드는 것을 방지 read.table 텍스트의 다양한 공간)

aggregate(dat[ , c("sales", "profits", "assets", "marketvalue")], # cols to aggregate 
      dat["country"],           # group column 
      FUN=mean)       # aggregation function 
관련 문제