how to calculate mean/median per group in a dataframe in r [duplicate]

library(dplyr) dat%>% group_by(custid)%>% summarise(Mean=mean(value), Max=max(value), Min=min(value), Median=median(value), Std=sd(value)) # custid Mean Max Min Median Std #1 1 2.666667 5 1 2.5 1.632993 #2 2 5.500000 10 1 5.5 6.363961 #3 3 2.666667 5 1 2.0 2.081666 For bigger datasets, data.table would be faster setDT(dat)[,list(Mean=mean(value), Max=max(value), Min=min(value), Median=as.numeric(median(value)), Std=sd(value)), by=custid] # custid Mean Max Min Median … Read more

Python Pandas : group by in group by and average?

If you want to first take mean on the combination of [‘cluster’, ‘org’] and then take mean on cluster groups, you can use: In [59]: (df.groupby([‘cluster’, ‘org’], as_index=False).mean() .groupby(‘cluster’)[‘time’].mean()) Out[59]: cluster 1 15 2 54 3 6 Name: time, dtype: int64 If you want the mean of cluster groups only, then you can use: In … Read more