I have to do a calculation of quartiles, but I need to group the data by two different conditions. First of all I need to group them by which position they hold in the company, but then I also need to group them by which union they are a part of. Here is an example of my data:
Position Union Salary
Consultant A 1000
Receptionist B 700
Consultant A 1250
Consultant A 1200
HR A 1100
HR B 800
Receptionist B 750
Student B 200
HR B 700
Consultant A 900
Student B 300
HR A 1500
Consultant A 1300
Consultant B 800
Consultant A 1300
Receptionist B 780
Student B 250
Consultant B 950
HR A 1150
Consultant A 1275
I've tried a number of different ways to go about this, including some very early tests with ddply, but I've also tried out summarise for instance:
library(dplyr)
x %>%
group_by(Union, Position) %>%
summarise(Salary= quantile(Salary))
Can anyone help me with this?
EDIT: several users helped me out with a great solution in the comments, thank you very much. I have an additional question though:
I also need to calculate the mean of the salaries by the same conditions.
I tried using the code akrun provided (which worked very well on the quartiles), but when I did this for the mean (x <- x %>% group_by(Union, Position) %>% do(data.frame(., as.list(mean(.$Salary))))
) it delivered a variable for each group, instead of merging into one variable and adding it at the end.
Can anyone tell me some way to fix this?