按类别显示 table R 中的平均值

Show average value in table R by categories

我想清理我的数据,但我是 R 的新手。

我的代码:

#library

library(dplyr)
library(shiny)
library(shinythemes)
library(ggplot2)

#Source
dataset <- read.csv("Wagegap.csv")

SFWage <- dataset %>% select(gender,JobTitle,BasePay,Year,)

clean <- na.omit(SFWage)


#UI
ui <- fluidPage(
  
  theme = shinytheme("united"),
  
  navbarPage("San Fransisco Wages",

             tabPanel("Barplot",
                    
                      mainPanel(
                        plotOutput("barplot")
                        
                      )) ,
             tabPanel("Table", 
                      mainPanel(
                        dataTableOutput("table")
                      ))                    
  )
  
)                                 
 
#server
server <- function(input, output){
  
  output$barplot <- renderPlot({
    
    ggplot(clean, aes(x = JobTitle, y = BasePay  ))+
    geom_bar(stat="Identity", width = 0.3, fill="orange")+
    labs(x= "Jobs", y = "Wage", title = "Wage per job")
   
})
   
  output$table <- renderDataTable({
    clean
    
  })
}
  shinyApp(ui = ui, server = server)

我得到 table 的输出: Table Output

我想要的是将 Jobtitle 捆绑并仅按性别分开,并显示 BasePay 的平均值。

M - 账户文员 - averageBasePay - 年

F - 账户文员 - averageBasePay - 年

每项工作依此类推。

此数据将用于比较数据集中给定的每项工作的性别工资差距。

P.S.

如果有人也能告诉我为什么 na.omit 不能清理空的性别,那就太棒了:)

您可以使用 group_by()summarise()

SFWage <- dataset %>% 
  group_by(gender,JobTitle, Year) %>%
  summarise(averageBasePay = mean(BasePay, na.rm=TRUE) %>%
  select(gender, JobTitle, averageBasePay, Year)