Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate mean of values per unique class

I have a dataframe:

sex   age
f     10
m     12
m     11
m     17
f     13
f     12
I     8

Want I want to calculate the mean of age per sex:

f=> mean age = (10+13+12) /3
m=> mean age = (12+11+17) /3
I=> mean age = 8

I am trying something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

combine(df, :age => mean => :mean_age, :sex => unique)

But all mean_age have the same value.

>Solution :

use groupby first:

combine(groupby(df, :sex), :age => mean => :mean_age)

or using DataFramesMeta.jl

@chain df begin
    groupby(:sex)
    @combine(:mean_age = mean(:age))
end
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading