如何通过col2找到col1分组的平均值

时间:2012-08-30 01:06:54

标签: r group-by average mean

  

可能重复:
  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 country

我试图通过使用R.中的另一列来查找列的平均值。它在SQL中很容易实现,但无法找到正确的函数并实现它们。以下是一些示例数据。

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

1 个答案:

答案 0 :(得分:2)

使用名为dat的data.frame看起来像:

     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