options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(fig.align="center", fig.width=5, fig.height=5, warning = FALSE, message = FALSE)
library(xaringanthemer)
duo_accent(
  primary_color = "ivory",
  secondary_color = "#310A31",
  header_font_google = google_font("Roboto", "400"),
  text_font_google   = google_font("Lato", "300"),
  code_font_family = "Fira Code",
  code_font_url = "https://cdn.rawgit.com/tonsky/FiraCode/1.204/distr/fira_code.css",
  header_color = "#f54278",
  title_slide_text_color = "#354a66"
)

What is dplyr?

--

--


The common pattern

Each of these functions meet the following:


filter: specifying row conditions

How do we get: * all individuals in either the Police or Fire Departments

--

library(workshop)
library(dplyr)
chi2 <- filter(chi_emps, Dept %in% c("POLICE", "FIRE"))

filter

How do we get all individuals meeting the following: * in either the Police or Fire Departments * salaried

chi3 <- filter(chi_emps, Dept %in% c("POLICE", "FIRE"),
               SalHour == "Salary")

select

What if we wanted to keep only the columns of chi3 related to salaried employees

chi4 <- select(chi3, Name, Titles,  Dept, FullPart, AnnSalary)

or

--

chi5 <- select(chi3, -SalHour, -HourlyRate, -TypicalHours)

--

Are chi4 and chi5 equivalent data sets?


arrange

How do we sort by salary?

chi6 <- arrange(chi4, AnnSalary)
chi7 <- arrange(chi4, desc(AnnSalary))

group_by and summarise


Example

What is the median salary in each department?

grp_sal <- group_by(chi_emps, Dept)
avg_sals <- summarise(grp_sal, 
                AvgSalary = mean(AnnSalary, na.rm = TRUE))
avg_sals <- summarise(grp_sal, 
                      AvgSalary = mean(AnnSalary, na.rm = TRUE),
                      numEmps = n())

What

reactable::reactable(avg_sals, )


thisisdaryn/workshop documentation built on Jan. 17, 2020, 7:31 p.m.