# Grouping and finding mean in R dataframe

## Grouping and finding mean in R dataframe

Contents

Problem Description:

I have a table like this:

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:

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 |>
summarize(full_time_male = mean(Salary[genderTime == "ftMale"]),
part_time_male = mean(Salary[genderTime == "ptMale"]))
``````

## Solution – 2

Another dplyr approach:

``````df %>%
summarise(Salary = mean(Salary, na.rm=TRUE)) %>%
ungroup() %>%
pivot_wider(names_from = genderTime, values_from = Salary, values_fill = 0) %>%

# A tibble: 3 × 3
<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")) %>%
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",