找出R中年份和公司名称的平均价值

问题描述 投票:-1回答:1

下面是我的数据集示例,我需要找到公司一年的平均收费率。

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
sql r group-by statistics aggregate
1个回答
1
投票

只需使用avg()

select date,company, avg(close)
from table group by date,company
© www.soinside.com 2019 - 2024. All rights reserved.