简体   繁体   中英

How to calculate the average of a column using other columns as a reference using excel

I have three columns in excel year , month value .

I want to average value considering month and year . In R language this function is done by group_by(). In excel how could this be done?

year    month   value
2019    1   12
2019    1   34
2019    2   56
2019    2   15
2020    1   16
2020    3   67
2020    4   89
2018    6   123
2018    6   45
2018    7   98
2019    3   53
2019    1   23
2020    1   12
2020    3   1

If one has Office 365 we can use:

=LET(
    y,A2:A15,
    m,B2:B15,
    v,C2:C15,
    u,SORT(UNIQUE(CHOOSE({1,2},y,m)),{1,2}),
    CHOOSE({1,1,2},u,AVERAGEIFS(v,y,INDEX(u,0,1),m,INDEX(u,0,2))))

Put this in the first cell and it will spill the results.

在此处输入图像描述

Once the HSTACK is release we can replace the CHOOSE with it:

=LET(
    y,A2:A15,
    m,B2:B15,
    v,C2:C15,
    u,SORT(UNIQUE(HSTACK(y,m)),{1,2}),
    HSTACK(u,AVERAGEIFS(v,y,INDEX(u,0,1),m,INDEX(u,0,2))))

Averageifs would do what you want, but you might want to review using the Filter function to duplicate the Group_By() method for other similar procedures. Once grouped, you can sum/average/sort, etc.

Averageifs:

=AVERAGEIFS(C:C,A:A,2018,B:B,6)

Filter:

=filter(C:C,(A:A=2018)*(B:B=6))
=Average(filter(C:C,(A:A=2018)*(B:B=6)))

See this spreadsheet for examples of both . I realize you're using Excel, but these formulas should work on both (though they are not the same)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM