Grouping and finding mean in R dataframe

Grouping and finding mean in R dataframe

Problem Description:

I have a table like this:

genderTimeSalaryGrade
ftMale32000G
ptMale15500DP
ptMale37500H
ptFemale31500G
ftFemale37400H
ftMale36000G
ptFemale31000G
ptFemale16000DP
ptMale37000H

I have the Grades as a factor where grades = DP, G, H

I need to create a table that gives me the mean salary by grade for full-time males (ftMale) and part-time males (ptMale) like so:

GradeFull-time MalePart-time male
DP015500
G340000
H037250

My dataset is a lot larger than this and there would be means for part time and full time in each grade. Any help would be much appreciated!
Thanks

Solution – 1

Group by Grade and then summarize your data by getting the mean of Salary while making sure that you are subsetting to the relevant ftMale/ptMale values.

library(tidyverse)

df |>
  group_by(Grade) |>
  summarize(full_time_male = mean(Salary[genderTime == "ftMale"]),
            part_time_male = mean(Salary[genderTime == "ptMale"]))

Solution – 2

Another dplyr approach:

df %>% 
  group_by(genderTime, Grade) %>% 
  summarise(Salary = mean(Salary, na.rm=TRUE)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = genderTime, values_from = Salary, values_fill = 0) %>% 
  select("Grade", "ftMale", "ptMale")

# A tibble: 3 × 3
  Grade ftMale ptMale
  <chr>  <dbl>  <dbl>
1 H          0  37250
2 G      34000      0
3 DP         0  15500

Just another (slightly different) dplyr solution

df %>% 
  filter(genderTime %in% c("ftMale", "ptMale")) %>% 
  group_by(genderTime, Grade) %>% 
  summarise(Salary = mean(Salary, na.rm=TRUE)) %>% 
  pivot_wider(names_from = genderTime, values_from = Salary, values_fill = 0)

The same using full R base:

reshape(aggregate(Salary ~ genderTime + Grade, 
                  subset = genderTime %in% c("ftMale", "ptMale"), 
                  FUN=mean, 
                  data=df), 
        direction = "wide", 
        idvar = "Grade", 
        timevar = "genderTime")
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject