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" )
subsetting
--
sorting
--
adding new variables
aggregating data
Each of these functions meet the following:
the first argument is a data frame
there are a variable number of following arguments
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"))
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")
What if we wanted to keep only the columns of chi3 related to salaried employees
chi4 <- select(chi3, Name, Titles, Dept, FullPart, AnnSalary)
--
chi5 <- select(chi3, -SalHour, -HourlyRate, -TypicalHours)
--
Are chi4 and chi5 equivalent data sets?
How do we sort by salary?
chi6 <- arrange(chi4, AnnSalary)
chi7 <- arrange(chi4, desc(AnnSalary))
group_by: partitions the data frame into groups by adding some invisible group information
summarise: aggregates data for each group
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())
reactable::reactable(avg_sals, )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.