在R中找到年份和公司名称的关闭率的平均值

时间:2019-03-17 16:39:54

标签: sql r group-by statistics aggregate

以下是我的数据集样本,我需要找到公司一年的平均关闭率。

Date    Close   company
2008    49.220001   Allstate Insurance Group
2008    47.73   Allstate Insurance Group
2008    48.060001   Allstate Insurance Group
2008    50.360001   Allstate Insurance Group
2009    21.67   Allstate Insurance Group
2009    16.83   Allstate Insurance Group
2011    31.139999   Allstate Insurance Group
2011    31.780001   Allstate Insurance Group
2011    31.780001   Allstate Insurance Group
2008    42.130001   ANTHEM
2009    41.450001   ANTHEM
2009    33.919998   ANTHEM
2009    37.970001   ANTHEM
2010    48.93   ANTHEM
2010    50.720001   ANTHEM
2010    49.66   ANTHEM
2012    18.219999   AXA inaurance
2013    18.450001   AXA inaurance
2014    26.309999   AXA inaurance
2014    25.950001   AXA inaurance

现在,我需要找到每个公司每年的平均值。例如,我需要找到每年“ 2008、2009、2010”的“ ANTHEM”平均值。不知道如何在聚合函数中传递2个变量。

我的预期输出是:

2008    42.130001   ANTHEM
2009    37.6849995  ANTHEM
2010    49.77000033 ANTHEM
2008    48.84250075 Allstate Insurance Group
2009    19.25   Allstate Insurance Group
2011    31.566667   Allstate Insurance Group

1 个答案:

答案 0 :(得分:1)

只需使用avg()

select date,company, avg(close)
from table group by date,company