0

I have a data set with both Age and Salary for 1.8m members aged between 18 and 70. Can you help me out with coding the median salary for each age 18 to 70. It would be nice to have three columns of output:

Age Number of Members at that Age Median salary at that Age

Any help would be much appreciated

Dane

Dane
  • 19
  • 2
  • Please add data using `dput` and show the expected output for the same. Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). Apart from that, this might be of help : https://stackoverflow.com/questions/25198442/how-to-calculate-mean-median-per-group-in-a-dataframe-in-r – Ronak Shah Mar 23 '20 at 02:31
  • Use the "mean by group" FAQ, but replace `mean` with `median`. Many options there, depending on how you value performance vs readability vs dependencies. You can similarly add on something to calculate the number in the group by using `length` instead of `median`, though packages like `data.table` or `dplyr` have specialty functions for that. – Gregor Thomas Mar 23 '20 at 04:52
  • One possible solution: `library(dplyr); your_data %>% group_by(age) %>% summarize(med_salary = median(salary), n_members = n())`. – Gregor Thomas Mar 23 '20 at 04:54

0 Answers0