Tidyverse Functions (Part 1)

Introduction

An incredibly useful metapackage for data journalists is tidyverse. This metapackage loads a handful of packages originally authored by Hadley Wickham that help users load data, clean them, reshape them, and aggregate them in different ways.

In this tutorial, I will outline some of the key functions from the dplyr package (which is part of tidyverse).

First, here is a summary of the functions covered in this tutorial and a quick description of when you’ll want to use each:

  • Use pipes (%>%) whenever you want to pass information to another function (i.e., when you’re adding a step to your analysis)

  • Use select() whenever you want to only include information about certain variables (columns)

  • Use filter() whenever you want to only include observations (rows) that match specified criteria

  • Use count() whenever you want to count how often a unique value comes up (e.g., how many times Rodrigo Zamith appears within a given variable)

  • Use arrange() whenever you want to sort the data (e.g., from high to low)

  • Use summarize() whenever you want to reduce many values to a single one (e.g., get the median value)

  • Use group_by() whenever you want to apply an operation separately for each value within a variable (e.g., every different department)

  • Use ggplot() whenever you want to create data visualizations

Our dataset for this tutorial

I will be illustrating each function by applying them to a dataset of gross salaries for Town of Amherst employees in 2020. These data were extracted from the PDF file using Tabula and exported to this CSV file.

I can load this dataset by using the read_csv() function from the readr package (part of tidyverse), supplying the URL for the dataset as my lone argument.

options(scipen=999)
library(tidyverse)
salaries <- read_csv("https://dds.rodrigozamith.com/files/amherst_gross_wages_2020.csv")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──

## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.3     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1

## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

## Rows: 802 Columns: 4

## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): last_name, first_name, department
## dbl (1): gross_wages

##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

You might be wondering what the options(scipen=999) line does. It simply disables scientific notation (which R will use for large values by default) because I find it easier to read 1900000000 than 1.9e+09.

Let’s review the first few rows in the dataset to see what we’re working with.

head(salaries)
last_name first_name gross_wages department
AARONSON JEREMY 252.71 Fire Department
ABDEL-MAKSOUD AMMAR 1433.96 Amherst Recreation
ABELLI NICOLE 57203.87 Amherst Recreation
ABRAMSON ANDREW 81.96 Town Clerk’s
AHO PATRICIA 180.18 Town Clerk’s
ALDRICH SONIA 132251.42 Accounting

As we can see, there are four variables in this dataset (and at least six observations).

Piping

The tidyr package (part of tidyverse) gives us access to a special kind of operator (%>%) that permits us to send the output (or result) of one line of code forward to the next line of code. In effect, what this does is pre-fill the first argument of the function appearing in the line of code following the pipe.

Consider the following code:

head(salaries, n=10)

In that example, we are supplying the head() function with two arguments. The first argument is unnamed; unless specified otherwise, head() assumes that the first argument you are providing it is for an attribute it calls x (which is expected to be the name of an object in our environment). Put another way, the above code is functionally equivalent to head(x=salaries, n=10). The x argument is required; without it, head() will give you an error:

head()
## Error in checkHT(n, dx <- dim(x)): argument "x" is missing, with no default

Then, by adding a comma, we conveyed that we’d be specifying a second argument. That second argument is n, which tells the head() function to show me the first X rows instead of six (the default). By specifying the number 10 (n=10), I am telling it to show me the first 10 rows.

If you want to see all of the arguments the head() function accepts, use the help system in RStudio or just type ?head into the Console. Scroll down to the section on “Arguments.”

Returning to the piping mechanism, we can use it as follows. First, we’ll load the salaries object. Then, we’ll use the piping operator to pipe the output of that line of code (all of the information about the salaries object) to another function. In the next line, we write out the head() function, but we only supply the second argument (n=10). That’s because the piping operator is pre-filling the first argument (x) that head() expects. The result is equivalent to what we saw above.

salaries %>%
  head(n=10)
last_name first_name gross_wages department
AARONSON JEREMY 252.71 Fire Department
ABDEL-MAKSOUD AMMAR 1433.96 Amherst Recreation
ABELLI NICOLE 57203.87 Amherst Recreation
ABRAMSON ANDREW 81.96 Town Clerk’s
AHO PATRICIA 180.18 Town Clerk’s
ALDRICH SONIA 132251.42 Accounting
ALDRICH MONICA 96169.00 Fire Department
ALLAN HENRY 69565.75 Jones Library
ALVAREZ MARTA 270.27 Town Clerk’s
ANAYA AMY 86178.46 Jones Library

While this approach may not seem particularly useful on such a simple example, it becomes extremely useful when you start chaining multiple functions together. Put another way, instead of nesting a bunch of functions inside one another in a way that is difficult to read (e.g., arrange(summarize(group_by(filter( ...​ )))))), you can place each function (step in the analysis) on its own line and just pipe the output from one function to the next.

This produces cleaner code that is much easier to follow. (Consequently, it makes it easier to avoid mistakes and to think logically about problem-solving.)

select()

The select() function is used to only include certain variables (columns) in the output. (Put another way, you are dropping the non-selected variables.) This is particularly useful when we are looking at the dataset ourselves. For example, if you have a lot of variables but only care about a few of them, you’ll want to use select(). Additionally, this function can be quite useful in advanced applications, as we’ll see later in the book.

With the select() function, you simply need to list the variables you are interested in (separating them with commas).

For example, let’s use that function to only examine the first_name, last_name, and department variables in our dataset:

salaries %>%
  select(first_name, last_name, department)
first_name last_name department
JEREMY AARONSON Fire Department
AMMAR ABDEL-MAKSOUD Amherst Recreation
NICOLE ABELLI Amherst Recreation
ANDREW ABRAMSON Town Clerk’s
PATRICIA AHO Town Clerk’s
SONIA ALDRICH Accounting
MONICA ALDRICH Fire Department
HENRY ALLAN Jones Library
MARTA ALVAREZ Town Clerk’s
AMY ANAYA Jones Library
CATHERINE ANDERSON Insepection Services
MARIAH ANTHONY Dispatch
EDWARD APPEL Town Clerk’s
PATRICK ARBOUR Fire Department
BRUCE ARBOUR Fire Department
JESUS AROCHO Police Department
SIMONE ASCHER Dispatch
CYNTHIA ASEBROOK Town Clerk’s
CAROL ASHBY Town Clerk’s
SHERRILL ASHTON Town Clerk’s
THERESA ATTERIDGE Jones Library
SUSAN AUDETTE Town Clerk’s
SIMONE AUDETTE Town Clerk’s
JARRETT AUSTIN Fire Department
DANIEL AVERILL Fire Department
CATHY AXELSON-BERRY Town Clerk’s
NANCY BAIR Town Clerk’s
BRIANNA BAKER Fire Department
DANIEL BALLANTINE Town Clerk’s
CALEB BALLANTINE Town Clerk’s
BRYANNYAGARA BARBOZA Town Clerk’s
ERIK BARDWELL Public Works
MOHAMAD BARHAM Town Clerk’s
MICHAEL BARONE Police Department
LAURA BARRY Dispatch
ROBIN BARTON Public Works
CHRISTOPHER BASCOMB Fire Department
GREGORY BASCOMB Senior Center
JOSEPH BASS Town Clerk’s
BAILEY BATTY Town Clerk’s
EVELYN BEAURY Town Clerk’s
PHILIP BELANGER Public Works
ANYA BENSON Amherst Recreation
ELAYNE BERGER Town Clerk’s
EMERY BERGER Town Clerk’s
CASEY BERGERON Fire Department
KIMBERLY BERGERON Public Works
EVERETT BERGMANN Public Works
LIZA BERNARD Public Works
CLARE BERTRAND Town Clerk’s
MATTHEW BERUBE Jones Library
JOHN BERWALD Town Clerk’s
HANNAH BETE Amherst Recreation
JUDITH BEYER Town Clerk’s
LOUIS BEYER Town Clerk’s
BARBARA BILZ Amherst Recreation
LAWRENCE BIRKHOLZ Senior Center
CAROL BIRTWISTLE Town Clerk’s
HOLLY BLACK Town Clerk’s
MARILYN BLAUSTEIN Town Clerk’s
JEFFREY BLAUSTEIN Town Clerk’s
TAYLOR BLOW Police Department
PAUL BOCKELMAN Town Manager’s Office
PARIS BOICE Jones Library
JANDALL BOOM Public Works
BRADLEY BORDEWIECK Conservation
MICHELLE BORKHUIS Fire Department
TYLER BORRELL Amherst Recreation
ZITA BOSCHER Police Department
CHERYL BOUCHER Collectors
THERESA BOUTHILETTE SARNA Assessor
HOLLY BOWSER Accounting
JEFFREY BOWSER Public Works
ROXANNE BOYD Jones Library
PAUL BRACCIOTTI Town Clerk’s
ANDREW BRACE Public Works
TYLER BRADLEY Town Clerk’s
PHILIP BRAININ Amherst Recreation
AARON BRAITHWAITE Amherst Recreation
BENJAMIN BREGER Planning
CHRISTINE BRESTRUP Planning
JILLIAN BREVIK Town Clerk’s
ALISA BREWER Town Council
ROBERT BROOKS Town Clerk’s
JENNIFER BROWN Public Health
CATHERINE BROWN Amherst Recreation
JOHN BRYAN Town Clerk’s
KELLEY BRYANT Town Clerk’s
ABIGAIL BUCEY Police Department
ZACHARY BULLOUGH Police Department
NONNY BURACK Senior Center
NONNY BURACK Town Clerk’s
NATHANAEL BURGESS Fire Department
DAVID BURGESS Assessor
CONOR BURKE Public Works
TRISTAN BURKE Amherst Recreation
GLENN BURROWS Public Works
MIA CABANA Jones Library
CARL CAIVANO Town Clerk’s
JONATHAN CALI Amherst Recreation
STEPHEN CALL Public Works
TREVOR CAMPBELL Town Clerk’s
EVAN CANNEY Police Department
ELISABETH CANTOR Town Clerk’s
KRISTA CARAWAY Public Works
ROBERT CARKHUFF Police Department
PETER CARLSON Public Works
BENTON CARR Police Department
LINDSAY CARROLL Police Department
CYNTHIA CARY Collectors
KAREN CASSIDY Jones Library
LAURA CASTANO-LONGEY Jones Library
NALEYAH CESAR-BONE Amherst Recreation
STEVEN CHANDLER Fire Department
NICHOLAS CHANDLER Police Department
DAEL CHAPMAN Town Clerk’s
KATHLEEN CHILDRESS Town Clerk’s
MATTHEW CHIPMAN Fire Department
NICHOLAS CHIREKOS Fire Department
MICHELE CHMURA Senior Center
JOHN CHUDZIK Police Department
ELIZABETH CHUDZIK Dispatch
STEPHANIE CICCARELLO Conservation
JOSHUA CICIA Dispatch
RICHARD CILIBERTO Fire Department
DANTE CITINO Amherst Recreation
TOMMY CLAIRE Fire Department
THOMAS CLARK Police Department
JOSHUA CLARK Public Works
CHARLES CLARK Public Works
BRUCE CLEVELAND Facilites/Maintenance
DAVID CLOONEY Fire Department
ANTHONY CLOUGH Amherst Recreation
DAVID CODY Insepection Services
JOHN COELHO Amherst Recreation
JOSEPH COFFEY Fire Department
JOSHUA COLBY Public Works
PAUL COLLINS Town Clerk’s
EMMA CONNER Town Clerk’s
DARBY CONNOLLY Amherst Recreation
LOUIS CONOVER Town Clerk’s
CLARE COOKE Jones Library
RUSSELL COOMBS Public Works
ADAM CORMIER Fire Department
DEBRA CORMIER Facilites/Maintenance
DOMINICK CORSETTI Police Department
ANDREW COTE Public Works
CASSANDRA COTE Fire Department
JESSE CRAFTS-FINCH Town Clerk’s
SARAH CRAIG Town Clerk’s
CHRIS CRANE Jones Library
JOHN CROWLEY Public Works
ROBERT CROWNER Town Clerk’s
PETER CRUMP Police Department
MATTHEW CRUTCH Public Works
RITA CURLEY Police Department
MICHAEL CURTIN Dispatch
WILLIAM CUTRONE Fire Department
BRIAN DALY Police Department
JESSICA DAMON Police Department
JAMES DAMOURAS Police Department
ANGELA DANGER Fire Department
KELLY DAPPRICH Town Clerk’s
SAMUEL DATTILO Conservation
MATTHEW DAVIES Public Works
SHYLA DAVIS Public Works
PATRICIA DE ANGELIS Town Council
MARY DECKER Facilites/Maintenance
DOUGLAS DEDISCHEW Town Clerk’s
ANTHONY DELANEY Accounting
SARAH DEMAREST Jones Library
PETER DEMLING Town Council
COREY DERES Public Works
MICHELE DESABRAIS Jones Library
JACK DESAUTELS Amherst Recreation
PAUL DETHIER Public Works
CHRISTOPHER DIAZ Town Clerk’s
JOSE DIAZ-GUTIERREZ Police Department
DAVID DION Fire Department
WAVERLY DOHERTY Amherst Recreation
MARY DOHRMANN Town Clerk’s
THERESA DONOHUE Senior Center
SULLIVAN DOUGLAS Police Department
RICHARD DOWNIE Police Department
JANICE DOYAMA Jones Library
MICHAEL DOYLE Fire Department
MARLENE DOYLE Town Clerk’s
EMMA DRAGON Public Health
RICHARD DUDKIEWICZ Information Technology
ELIZABETH DUFFY Assessor
DARCY DUMONT Town Council
KATHERINE EDELL Police Department
KYLE ELLIOTT Police Department
JANET ENGELSON Town Clerk’s
ERIC ENGELSON Town Clerk’s
MEREDITH ENRIGHT Town Clerk’s
SOPHIA EPSTEIN Town Clerk’s
TIMOTHY EVE-COWLES Fire Department
STEPHANIE EVERETT Dispatch
THOMAS FAIR Jones Library
MOLLY FALSETTI-YU Town Clerk’s
MOLLY FARBER Police Department
ANABELLE FARNHAM Amherst Recreation
JULIA FARNHAM Amherst Recreation
ANABELLE FARNHAM Town Clerk’s
HUMERA FASIHUDDIN Town Clerk’s
JULIE FEDERMAN Public Health
SERGE FEDOROVSKY Information Technology
FELIPE FELICIANO Police Department
YVONNE FELICIANO Police Department
ADAM FELTMAN Public Works
STEPHEN FELTOVIC Public Works
DAMARIS FERNANDEZ-SIERRA Amherst Recreation
PAMELA FIELD-SADLER Planning
JANE FINDLAY Town Clerk’s
JERRY FISCHER Town Clerk’s
ROBERT FLAHERTY Fire Department
THERESA FLEURENT Accounting
RICHARD FLEURENT Facilites/Maintenance
GARY FLYNN Fire Department
BRYAN FORBES Public Works
BARBARA FORD Town Clerk’s
DIANA FORDHAM Amherst Recreation
SARAH FORSAITH Fire Department
JON FOSTER Public Works
AVA FRADKIN Town Clerk’s
KATHARINE FRETWELL Town Clerk’s
LENORE FRIEDLANDER Town Clerk’s
ELAINE FRONHOFER Town Clerk’s
SEAN FROST Public Works
MATTHEW FRYDRYK Police Department
TANYA FULLER Town Clerk’s
VICTORIA GADDY Jones Library
CHARLES GAGNON Amherst Recreation
SCOTT GALLAGHER Police Department
RYANN GALLANT Town Clerk’s
DAVID GANCORZ Public Works
STEPHEN GAUGHAN Fire Department
CHERYL GAUTHIER Public Works
ANA GAUTHIER Town Clerk’s
SCOTT GAVRON Public Works
DOUGLAS GEARY Police Department
HENRY GEFFERT Amherst Recreation
CALEIGH GEOGHEGAN Amherst Recreation
DAN GEORGAKAS Senior Center
STEPHEN GEORGE Town Clerk’s
GENNADI GERSTNER Police Department
DANIEL GESSEN Fire Department
GEORGE GILLISPIE Fire Department
JULIEN GINSBERG PELTZ Town Clerk’s
ADELE GLADSTONE-GILBERT Town Clerk’s
LAUREL GLOCHESKI Town Clerk’s
WILLIAM GLOVER Information Technology
LUIS GOMBA Amherst Recreation
CEDRIC GONNETT Amherst Recreation
CHRISTOPHER GOODHIND Fire Department
SUSAN GOODHIND Collectors
STEPHANIE GOODWIN Amherst Recreation
FRAN GOODWIN Town Clerk’s
DEBORAH GORDON Fire Department
BETH GOREN Town Clerk’s
ETHAN GORMAN Fire Department
PATRICK GORMELY Public Works
HELEN GRABEL Town Clerk’s
BENJAMIN GRAHAM Fire Department
JASON GRANT Public Works
REILLY GRANT Town Clerk’s
NIAMH GRAY-MULLEN Jones Library
JULIE GRDEN Town Clerk’s
CHRISTOPHER GREENFIELD Fire Department
JUDY LYNN GRIESEMER Town Council
SHIRLEY GRIFFIN Town Clerk’s
JOANN GRISWOLD Town Clerk’s
HEATHER GROSS Town Clerk’s
AISLINN GUINEE Police Department
RYAN GWYTHER Fire Department
DARRYL HAGAR Public Works
VIRGINIA HAMILTON Town Clerk’s
LOUISE HAMMANN Town Clerk’s
DONNA HANCOCK Senior Center
MANDI JO HANNEKE Town Council
SEAN HANNON Information Technology
CYNTHIA HARBESON Jones Library
MARY HARRAGHY Town Clerk’s
JOSHUA HARRIS Police Department
RITA HART Senior Center
FAIZAN HASSAN Amherst Recreation
JEFFREY HATCH Senior Center
RALPH HATHAWAY Facilites/Maintenance
AARON HAYDEN Town Clerk’s
WALTER HENRY Public Works
CAROL HEPBURN Animal Control
DEBORAH HERBERT Town Clerk’s
EUGENE HERMAN Senior Center
MARY HERMAN Senior Center
GEORGE HICKS-RICHARDS Jones Library
OLIVIA HILLMEYER Public Works
LEAH HIRSHBERG Town Clerk’s
JESSE HLAVA Town Clerk’s
MARY HOCKEN Town Clerk’s
KENNETH HOFFMAN Town Clerk’s
CHRIS HOFFMANN Town Clerk’s
PAUL HOLDEN Amherst Recreation
DAVID HOLMES Fire Department
DANA HOPKINS MCGILL Amherst Recreation
JEANNE HORRIGAN Town Clerk’s
MARY HUBBELL Town Clerk’s
SUSAN HUGUS Jones Library
ANDREW HULSE Police Department
MARCUS HUMBER Police Department
RALPH HURWITZ Jones Library
JOHN IMBIMBO Facilites/Maintenance
JOHN INGRAM Fire Department
CYRUS IRANI Amherst Recreation
DARIUS IRANI Amherst Recreation
JOSEPH ISABELLE Public Works
MICHAEL JACQUE Public Works
ERIN JACQUE Conservation
MARY JAFFEE Town Clerk’s
SARCENAS JEAN-PHILIPPE Amherst Recreation
NANCY JENAL Town Clerk’s
TAMMY JEZEK Jones Library
BRIAN JOHNSON Police Department
BROOKE JOHNSON Collectors
MARK JOHNSON Town Clerk’s
MARJORIE JOHNSON Town Clerk’s
JOY JOLIE Police Department
JAMES JORDAN Public Works
MARION JORDAN Amherst Recreation
NYINDU KABANGU Amherst Recreation
STEVEN KACEY Collectors
ARI KASAL Fire Department
MICHAEL KATZ Public Health
RACHAEL KATZ Amherst Recreation
MARC KEENAN Amherst Recreation
PHYLLIS KEENAN Town Clerk’s
JANE KELLEY Town Clerk’s
WYATT KELLMAN Town Clerk’s
BRENDAN KELLY Fire Department
SUSAN KELLY Town Clerk’s
DAVID KELSEN Fire Department
DONNA-RAE KENNEALLY Town Manager’s Office
JOHN KENNEDY Fire Department
REMINGTON KEYES Police Department
MATTHEW KING Jones Library
DEVON KING Town Clerk’s
SHOSHONA KING Town Clerk’s
ERIN KLAES Amherst Recreation
NOLAN KLAES Amherst Recreation
DUANE KLIMCZYK Public Works
DAVID KNIGHTLY Police Department
CHRISTINA KNIGHTLY Police Department
BRIAN KNIGHTLY Public Works
LOUIS KNOLLE Public Works
ERIC KNYT Fire Department
CAROL KOLENIK Town Clerk’s
KAREN KOWLES Senior Center
NOAH KRAMER Amherst Recreation
KAY KRANICK-WEINBERG Senior Center
GEOFFREY KRAVITZ Town Manager’s Office
ELIZABETH KROGH Town Clerk’s
MITCHELL KUC Police Department
DINAH KUDATSKY Town Clerk’s
OWEN KUPPERMAN Amherst Recreation
SHIRLEY KURTULUS Senior Center
DAVID LABANC Fire Department
CHRISTINE LABICH Town Clerk’s
ADAM LADD Fire Department
JENNIFER LAFOUNTAIN Collectors
JOSEPH LAGASSE Fire Department
NOAH LAMB Amherst Recreation
DERICK LAMOUREUX Police Department
JESSICA LAMSON Jones Library
TODD LANG Police Department
AMY LANGDON Town Clerk’s
JEREMIAH LAPLANTE Facilites/Maintenance
STEPHANIE LAPLANTE Police Department
WILLIAM LARAMEE Police Department
CATHERINE LARSON Amherst Recreation
MEHRENE LARUDEE Town Clerk’s
ELIZABETH LASS Conservation
EVAN LATVALLA Facilites/Maintenance
MARY JANE LAUS Town Clerk’s
JACK LAXSON Town Clerk’s
CAROLINE LEDERMAN Town Clerk’s
PAMELA LEDOUX Town Clerk’s
DIANNE LEDUC Facilites/Maintenance
MICHAEL LEFLAR Town Clerk’s
PHYLLIS LEHRER Town Clerk’s
MYRA LENBURG Town Clerk’s
SAMUEL LEONARD Amherst Recreation
ROBIN LEVINE Jones Library
KEYVIN LEWIS Fire Department
KALSANG LHAMO Town Clerk’s
TYLER LIBONATE Amherst Recreation
KRISTEN LINDBERG Fire Department
ELLEN LINDSEY Town Clerk’s
MARY LISSECK Senior Center
JOSEPH LISSECK Senior Center
KEVIN LITALIEN Fire Department
KIM LITTMANN Accounting
SCOTT LIVINGSTONE Police Department
KUEI LO Jones Library
JOHN LOEB Town Clerk’s
CHERYL LOFLAND Public Works
ELIZABETH LOFTUS Fire Department
KEITH LONGTO Public Works
GALE LONGTO Town Clerk’s
JANET LOPEZ Police Department
HEATHER LORD Town Council
SUSAN LOWERY Town Clerk’s
JOSEPH LUGO Town Clerk’s
JAILENE LYNCH Dispatch
AMY MEI LYNCH Town Clerk’s
ELIZABETH MABEE Town Clerk’s
ALEXANDER MACDONALD Police Department
RICHARD MACLEAN Police Department
HELEN MACMELLON Senior Center
DELANEY MACPHETRES Amherst Recreation
JOHN MAGARIAN Town Clerk’s
JANE MAHMOODI Senior Center
JACQUELINE MAIDANA Town Clerk’s
CADEN MAINZER Town Clerk’s
LEWIS MAINZER Town Clerk’s
JANE MAIRS Town Clerk’s
NATHANIEL MALLOY Planning
SUSAN MALONE Insepection Services
AIDAN MALONEY Amherst Recreation
PATRICK MANEY Fire Department
SEAN MANGANO Accounting
ARLENE MANNING Town Clerk’s
GRAZYNA MARCZUK Amherst Recreation
SHIRLEY MARKHAM Town Clerk’s
EMMA MARKHAM Town Clerk’s
JANET MARQUARDT Town Clerk’s
ANN MARIE MARQUIS Fire Department
SARAH MARSHALL Town Clerk’s
MARCIN MARSZALEK Jones Library
DAVID MARTELL Fire Department
MARTHA MARTENEY Town Council
SHAVENA MARTIN Town Clerk’s
AMBER MARTIN Town Clerk’s
TAYLOR MARTIN-GRAHAM Police Department
TYLER MARTINS Police Department
PATRICIA MASCIS Town Clerk’s
JOSEPH MASPO Police Department
BETSY MATHEWS Town Clerk’s
ELIZABETH MATTHEWS Jones Library
MICHELE MATUSZKO Police Department
JULIA MAWSON Town Clerk’s
STEVEN MCCARTHY Insepection Services
LAURIE MCCOMB Fire Department
LINDSEY MCCONNELL Town Council
WILLIAM MCCUTCHEON Amherst Recreation
ALLISON MCDONALD Town Council
JOHN MCDONALD Jones Library
BARBARA MCGLYNN Jones Library
VIANKA MCKENZIE Amherst Recreation
MADELEINE MCLAUGHLIN Jones Library
ELIZABETH MCLAUGHLIN Town Clerk’s
PATRICIA MCPEAK-LAROCCA Town Clerk’s
MICHAEL MEADE Town Clerk’s
MICHAEL MEAGHER Town Clerk’s
LAURA MELBIN Town Clerk’s
JOEL MELENDEZ Fire Department
WILLIAM MENARD Police Department
DANIEL MENARD Police Department
JOSEPH MERCIER Public Works
DOROTHY MERRIAM Town Clerk’s
ADAM METZGER Police Department
AUSTIN MILES Amherst Recreation
JOHN MILLER Fire Department
ANGELA MILLS Town Manager’s Office
SHALINI BAHL MILNE Town Council
DAVID MINER Fire Department
JOANNE MISIASZEK Town Manager’s Office
BRIAN MITCHELL Public Works
CHLOE MITCHELL Town Clerk’s
EDWARD MONE Town Clerk’s
TODD MONGEON Police Department
KAYDEN MOORE Town Clerk’s
GUILFORD MOORING Public Works
ALBERTO MORALES-FERNANDEZ Amherst Recreation
SHARON MORGAN Town Clerk’s
ROBERT MORRA Insepection Services
BRIAN MORRIS Fire Department
RICHARD MORSE Town Clerk’s
ALICIA MORTON Senior Center
THOMAS MORTON Senior Center
ANNABEL MOTT Amherst Recreation
STANLEY MOULTON Town Clerk’s
JENNIFER MOYSTON Town Manager’s Office
ISAIAH MOYSTON Amherst Recreation
JENNIFER MULLINS Planning
MARISA MULVEY Amherst Recreation
NANCY MURPHY Public Works
SHAWN MURPHY Amherst Recreation
LINDA MURRAY Town Clerk’s
NATALIE NADEAU Amherst Recreation
KATE NADOLSKI Amherst Recreation
KASEY NAGLE Police Department
OLIVIER NDIKUMANA Amherst Recreation
TIMOTHY NEALE Town Clerk’s
WALTER NELSON Fire Department
DEBORAH NEUBAUER Town Clerk’s
LINDA NEWCOMB Police Department
KATHERINE NEWELL Police Department
KATHERINE NEWMAN Police Department
CATHERINE NEWMAN Town Clerk’s
ANDREA NEWMAN Town Clerk’s
MARTIN NORDEN Town Clerk’s
ELIJAH NORMAN Amherst Recreation
CHRISTOPH NUESSLEIN Jones Library
STEFAN NUESSLEIN Jones Library
AMY NUSSBAUM Town Clerk’s
MARY OBRIEN Public Health
ROBERT O’BRIEN Insepection Services
CLAUDIA O’BRIEN Town Clerk’s
MARY O’CONNOR Town Clerk’s
MARY BETH OGULEWICZ Senior Center
ATHENA OKEEFFE Town Council
KELLY OLANYK Public Works
PATRICIA OLANYK Town Clerk’s
EMILY OLES Dispatch
JEFFERY OLMSTEAD Fire Department
ALANA O’LOUGHLIN Amherst Recreation
JOAN O’MEARA Town Clerk’s
LISA ORAM Town Clerk’s
ROBERT ORRELL Public Works
LIAM O’SULLIVAN Amherst Recreation
MIGUEL OTERO Jones Library
COURTNEY OUSLEY Public Works
LANCE OVERBY Town Clerk’s
CYNTHIA OWENS Town Clerk’s
LINDSAY PALMER Jones Library
DOROTHY PAM Town Council
JOSE PAREDES Jones Library
JANICE PARKER-RILEY Senior Center
JANICE PARKER-RILEY Town Clerk’s
TAMBETSU PARKS Town Clerk’s
JEFFREY PARR Fire Department
JOHN PASTORELLO Amherst Recreation
LUDMILLA PAVLOVA-GILLHAM Town Clerk’s
JUSTIN PAYAN Town Clerk’s
TYLER PEASE Conservation
DENIS PELLETIER Fire Department
COOPER PENNIMAN Amherst Recreation
MICHAEL PEREZ Public Works
JANICE PETERMAN Police Department
TERRY PETERS Town Clerk’s
JAMES PEWTHERER Town Clerk’s
BRIDGETTE PHILIBERT Amherst Recreation
SUSAN PHILLIPS Senior Center
JONATHAN PICKERING Public Works
JAMES PISTRANG Town Clerk’s
BARBARA PISTRANG Town Clerk’s
CAROLYN PLATT Jones Library
BROOKE PODSIADLO Amherst Recreation
ANTONIO POLINO Amherst Recreation
MAUREEN POLLOCK Planning
CHRISTOPHER PRICE Public Works
JESSICA PRONOVOST Dispatch
JOAN PYFROM Jones Library
ANDREA RAMIREZ FRANCO Police Department
LOUIS RANDAZZO Amherst Recreation
KAREN RANEN Senior Center
STEPHEN RANSFORD Senior Center
STEPHEN RANSFORD Town Clerk’s
VIOLET RAWLINGS Town Clerk’s
JAMIE REARDON Police Department
EMILY REARDON Town Council
EMILY REARDON Town Clerk’s
PATRICIA RECTOR Senior Center
PATRICIA RECTOR Town Clerk’s
MELINDA REID Town Clerk’s
DEVIN REILLY Police Department
TRISTAN RENTSCH Town Clerk’s
CATHERINE REPETTI Town Clerk’s
NORYN RESNICK Town Clerk’s
JENNIFER REYNOLDS Senior Center
IAN RHODEWALT Town Clerk’s
DALE RICE Town Clerk’s
MELISSA RICKER-HORTON Insepection Services
TATE RIETKIRK Amherst Recreation
MIGUEL RIVERA Dispatch
EVELYN RIVERA-RIFFENBURG Town Manager’s Office
LUCY ROBINSON Town Clerk’s
SARAH ROE Fire Department
ELLA ROMANELLI Town Clerk’s
AALIYAH ROSA Amherst Recreation
PAUL ROSA Fire Department
AIDEN ROSENBLATT Fire Department
EVAN ROSS Town Council
ANNA ROSSI Senior Center
ANNA ROSSI Town Clerk’s
JUAN ROSS-PERKINS Amherst Recreation
SETH ROTHBERG Jones Library
ELIZABETH ROWELL Town Clerk’s
MICHAEL ROY Fire Department
SUSAN ROZNOY Town Clerk’s
SOLOMON RUESCHEMEYER-BAILEY Town Clerk’s
JASON RUSHFORD Dispatch
AMY RUSIECKI Public Works
JANET RYAN Jones Library
GEORGE RYAN Town Council
RONALD RYCZEK Fire Department
JAYMES SALTIS Information Technology
BARBARA SALTZ Senior Center
HENRY SAMMIS Town Clerk’s
MIGUEL SANTIAGO Public Works
MANUEL SANTIAGO Town Clerk’s
THOMAS SARNA Parking Enforcement
JOHN SARNACKI Police Department
ANITA SARRO Town Clerk’s
JUSTIN SATKOWSKI Police Department
RYAN SAUNDERS Amherst Recreation
MICHAEL SAWICKI Fire Department
AMANDA SAYEGH Amherst Recreation
ALANNAH SCARDINO Fire Department
JENNA SCHILLING Amherst Recreation
APRIL SCHILLING Amherst Recreation
SOPHIE SCHILLING Amherst Recreation
CATHY SCHOEN Town Council
STEPHEN SCHREIBER Town Council
NANCY SCHROEDER Public Health
TRACEY SCHRYBA Insepection Services
SUDHA SETTY Town Clerk’s
MOHAN SETTY-CHARITY Town Clerk’s
BRANDON SEVIGNE Public Works
EMILY SHALLCROSS Town Clerk’s
AMANDA SHALLCROSS Town Clerk’s
JOHN SHANNON Jones Library
MARY SHARMA Town Clerk’s
SHARON SHARRY Jones Library
MARY ELLEN SHAUGHAN Senior Center
MARY SHAUGHAN Town Clerk’s
CHRISTINA SHEN Insepection Services
JACQUELINE SHERIDAN Town Clerk’s
CHRISTINE SHERWOOD Amherst Recreation
BRADLEY SHERWOOD Amherst Recreation
ELWOOD SHULAR Facilites/Maintenance
STUART SHULMAN Town Clerk’s
JAYANT SINGH Fire Department
JASON SKEELS Public Works
CHRISTOPHER SKEELS Public Works
BENJAMIN SKELTON Amherst Recreation
KAREN SKOLFIELD Town Clerk’s
DAVID SKRIBISKI Public Works
LISA SLOCUM Jones Library
ELI SLOVIN Police Department
EDMUND SMITH Insepection Services
JAMES SMITH Cherry Hill Golf Course
CHELSEA SMITH Fire Department
CALVIN SMITH Fire Department
RENATA SMITH Town Clerk’s
KATHLEEN SMITH Town Clerk’s
ALAN SNOW Public Works
JOHN SOBIESKI Police Department
CONNOR SORMANTI Police Department
MICHAEL SOSA Amherst Recreation
SCOTT SOVERINO Police Department
MARJORIE SPIEGEL Jones Library
LUCIA SPIRO Town Clerk’s
KERRY SPITZER Town Council
MATTHEW SPOSITO Fire Department
AMY SPRINGER Town Clerk’s
JOSEPH STAFFORD Public Works
ADRIAN STAIR Town Clerk’s
ANNA STANFORTH Town Clerk’s
CILO STEARNS Town Clerk’s
DIANA STEIN Town Clerk’s
ANDREW STEINBERG Town Council
VALERIE STEINBERG Jones Library
JOSHUA STEININGER Fire Department
JULIE STEPANEK Jones Library
SHIRLEY STEVENS Senior Center
BEVERLY STEVENS Town Clerk’s
JOEL STEVENS Town Clerk’s
ZELDA STEWART Police Department
MYRIAM STIVEN Amherst Recreation
LACE STOKES Jones Library
MARY STREETER Town Clerk’s
LINDSAY STROMGREN Fire Department
SARAH STROUD Jones Library
ANTRIESE SUAREZ Amherst Recreation
JALEN SULLIVAN Amherst Recreation
DELANEY SULLIVAN Police Department
DELANEY SULLIVAN Town Clerk’s
BRIANNA SUNRYD Information Technology
SARAH SWARTZ Town Council
HANNAH SWEET Dispatch
HAROLD SWIFT Police Department
CELESTE SZE Senior Center
MICHAEL SZEWCZYNSKI Fire Department
MICHAEL SZWED Public Works
J TAN Town Clerk’s
MARTHA TAUNTON Town Clerk’s
AMBER TAYLOR Public Works
STEVE TELEGA Public Works
ADRIENNE TERRIZZI Town Clerk’s
PAUL THEILMAN Fire Department
MAGGIE THIBAULT Amherst Recreation
REGAN THIBAULT Amherst Recreation
JON THOMPSON Insepection Services
MARK THOMPSON Public Works
ROBIN THOMPSON Town Clerk’s
LYNNE THOMPSON Town Clerk’s
WILLIAM THOMPSON Town Clerk’s
NIELS THOMSEN Fire Department
KIRSTEN THOMSON Town Clerk’s
SCOTT THURSTON Police Department
PERSIS TICKNOR-SWANSON Town Clerk’s
JANICE TIDLUND Public Works
PIERRE TILUS Amherst Recreation
GABRIEL TING Police Department
ALEJANDRO TORO-RODRIGUEZ Public Works
RAYMOND TORRES Dispatch
PETER TRIPP Town Clerk’s
DYLAN TUNNELL Fire Department
LORI TURATI Assessor
JACQUELINE TUTHILL Town Clerk’s
SUSAN TYLER Town Clerk’s
JOHN URSCHEL Town Clerk’s
SEAN VALENTINE Town Clerk’s
THOMAS VALLE Fire Department
KAYLAN VALOVCIN Fire Department
SARA VAN STEENBURGH Town Clerk’s
DENNIS VANDAL Senior Center
JO-ANNE VANIN Town Clerk’s
NATHANIEL VANNOY Public Works
FRANCES VANTREESE Town Clerk’s
ANDREW VECCHIO Public Works
ANDREW VECCHIO Fire Department
LANDON VENEY Fire Department
CATHERINE VERTS Jones Library
STEPHANIE VIGNONE Jones Library
JOSHUA VILLANUEVA Amherst Recreation
MICHAEL VISNIEWSKI Fire Department
KAYLEIGH VOCCA Information Technology
ABBE VREDENBURG Town Clerk’s
NICHOLAS WALAS Amherst Recreation
KATELYN WALAS Amherst Recreation
MICHAEL WARNER Information Technology
DAVID WASKIEWICZ Insepection Services
THOMAS WATERMAN Public Works
STEPHEN WATSON Police Department
GABRIELA WEAVER Town Clerk’s
RODNEY WEBER Cherry Hill Golf Course
RAFAEL WEIDENFELD Town Clerk’s
MARY WEIDENSAUL Jones Library
MAYA WEILERSTEIN Amherst Recreation
ROBERT WEINER Town Clerk’s
LYNNE WEINTRAUB Jones Library
DANO WEISBORD Town Clerk’s
DANIEL WELCH Police Department
LINDA WENTWORTH Jones Library
CHARLOTTE WESTHEAD Town Clerk’s
WILLIE WHEELER Police Department
KATHERINE WHITCOMB Jones Library
CHRISTINE WHITE Jones Library
PHYLLIS WHITNEY Senior Center
MARYANNA WHITTEMORE Town Clerk’s
CLIFFORD WILKINSON Fire Department
CRYSTAL WILLARD Public Works
ROBERT WILLIAMS Conservation
CAROLINE WILLIAMS Fire Department
RODNEY WILLIS Public Works
DAKOTA WILLIS Public Works
ELIZABETH WILLSON Public Works
NANCY WILSON Town Clerk’s
HENDER WINER Town Clerk’s
DAWN WINKLER Jones Library
SHEILA WINTER Town Clerk’s
DEBRA WIRTH Town Clerk’s
GREGORY WISE Police Department
MORGAN WITHERELL Town Clerk’s
ALBERT WOODHULL Town Clerk’s
DONNA WOODS Amherst Recreation
KRISTIN WOODS Police Department
WILLIAM WORTHLEY Dispatch
JOSEPH WORTHLEY Police Department
MARK WOYNAR Public Works
NATHANIEL WRIGHT Public Works
CAMERON WRIGHT Police Department
ELISE WRIGHT Police Department
THERESA WRIGHT Town Clerk’s
GORDON WYSE Town Clerk’s
TERRENCE YATES Public Works
GERALD YELLE Town Clerk’s
MATTHEW YODER Public Works
RONALD YOUNG Police Department
SASHA YOUNG Amherst Recreation
NOR YOUNG Amherst Recreation
TRACY ZAFIAN Town Clerk’s
ROBERT ZAKAITIS Public Works
CORY ZENG Jones Library
FAYE ZHANG Town Clerk’s
CHUNYU ZHOU Parking Enforcement
MATTHEW ZIOMEK Conservation
MATTHEW ZIOMEK Police Department
KAY ZLOGAR Town Manager’s Office
GEOFFREY ZUCKER Town Clerk’s

We now see a data frame object that has just those three variables (it omits the gross_wages variable).

This isn’t a big deal with a small dataset like this one. However, if you have a dataset with 50+ variables and want to take a look at just a few variables from the left and right ends of the dataset, this will prove quite helpful.

filter()

While select() is designed to include only certain variables (columns), filter() is designed to include only certain observations (rows) based on the criteria we specify. Use filter() when a question asks you to only include certain kinds of observations (e.g., when a variable is equivalent to, greater than, etc., a particular value).

For example, if we only want to include people from the Police Department, we’d use a filter that specifies an equivalency between our desired variable (department) and value (Police Department):

salaries %>%
  filter(department=="Police Department")
last_name first_name gross_wages department
AROCHO JESUS 162880.27 Police Department
BARONE MICHAEL 99440.02 Police Department
BLOW TAYLOR 645.86 Police Department
BOSCHER ZITA 1498.25 Police Department
BUCEY ABIGAIL 1269.20 Police Department
BULLOUGH ZACHARY 77077.13 Police Department
CANNEY EVAN 1096.46 Police Department
CARKHUFF ROBERT 431.20 Police Department
CARR BENTON 90851.04 Police Department
CARROLL LINDSAY 108957.81 Police Department
CHANDLER NICHOLAS 101155.31 Police Department
CHUDZIK JOHN 96244.37 Police Department
CLARK THOMAS 103902.69 Police Department
CORSETTI DOMINICK 100174.43 Police Department
CRUMP PETER 1513.28 Police Department
CURLEY RITA 90198.23 Police Department
DALY BRIAN 131630.02 Police Department
DAMON JESSICA 91391.38 Police Department
DAMOURAS JAMES 117500.49 Police Department
DIAZ-GUTIERREZ JOSE 90287.16 Police Department
DOUGLAS SULLIVAN 292.89 Police Department
DOWNIE RICHARD 3820.44 Police Department
EDELL KATHERINE 240.32 Police Department
ELLIOTT KYLE 1393.13 Police Department
FARBER MOLLY 80048.69 Police Department
FELICIANO FELIPE 106668.70 Police Department
FELICIANO YVONNE 92213.81 Police Department
FRYDRYK MATTHEW 101439.60 Police Department
GALLAGHER SCOTT 142611.97 Police Department
GEARY DOUGLAS 83344.78 Police Department
GERSTNER GENNADI 1727.31 Police Department
GUINEE AISLINN 1918.83 Police Department
HARRIS JOSHUA 89455.04 Police Department
HULSE ANDREW 92243.96 Police Department
HUMBER MARCUS 92377.35 Police Department
JOHNSON BRIAN 135750.79 Police Department
JOLIE JOY 65043.12 Police Department
KEYES REMINGTON 90.12 Police Department
KNIGHTLY DAVID 90392.65 Police Department
KNIGHTLY CHRISTINA 71373.52 Police Department
KUC MITCHELL 2910.60 Police Department
LAMOUREUX DERICK 646.80 Police Department
LANG TODD 172626.81 Police Department
LAPLANTE STEPHANIE 1366.83 Police Department
LARAMEE WILLIAM 98803.32 Police Department
LIVINGSTONE SCOTT 172109.78 Police Department
LOPEZ JANET 116041.14 Police Department
MACDONALD ALEXANDER 889.94 Police Department
MACLEAN RICHARD 116277.22 Police Department
MARTIN-GRAHAM TAYLOR 322.93 Police Department
MARTINS TYLER 101617.30 Police Department
MASPO JOSEPH 5429.82 Police Department
MATUSZKO MICHELE 68509.21 Police Department
MENARD WILLIAM 112471.56 Police Department
MENARD DANIEL 49403.20 Police Department
METZGER ADAM 1547.51 Police Department
MONGEON TODD 646.80 Police Department
NAGLE KASEY 95659.11 Police Department
NEWCOMB LINDA 2940.92 Police Department
NEWELL KATHERINE 80867.12 Police Department
NEWMAN KATHERINE 20049.16 Police Department
PETERMAN JANICE 713.90 Police Department
RAMIREZ FRANCO ANDREA 225.30 Police Department
REARDON JAMIE 138785.54 Police Department
REILLY DEVIN 630.84 Police Department
SARNACKI JOHN 51556.16 Police Department
SATKOWSKI JUSTIN 87633.08 Police Department
SLOVIN ELI 1051.40 Police Department
SOBIESKI JOHN 6899.20 Police Department
SORMANTI CONNOR 953.42 Police Department
SOVERINO SCOTT 89972.32 Police Department
STEWART ZELDA 1043.90 Police Department
SULLIVAN DELANEY 687.17 Police Department
SWIFT HAROLD 4919.84 Police Department
THURSTON SCOTT 87408.42 Police Department
TING GABRIEL 143698.34 Police Department
WATSON STEPHEN 60.08 Police Department
WELCH DANIEL 103487.72 Police Department
WHEELER WILLIE 574.48 Police Department
WISE GREGORY 121546.06 Police Department
WOODS KRISTIN 103.11 Police Department
WORTHLEY JOSEPH 51081.00 Police Department
WRIGHT CAMERON 2031.47 Police Department
WRIGHT ELISE 1336.84 Police Department
YOUNG RONALD 138974.80 Police Department
ZIOMEK MATTHEW 107948.39 Police Department

The equivalency operators most likely to be useful to you at this stage are:

  • == (is equivalent to)

  • != (is not equivalent to)

  • > (is greater than)

  • < (is less than)

  • >= (is greater than or equal to)

  • (is less than or equal to)

This function also allows us to specify multiple criteria (e.g., include people from the Police Department with the title Police Officer) using Boolean operators.

The Boolean operators that are most likely to be useful to you at this stage are:

  • & (AND), which is useful if we want a filter for observations where the individual’s gross_wages exceeds 100000 AND they work for in a department called Police Department

  • | (OR), which is useful if we want a filter for observations where the individual works for either the department called Police Department OR the department called Fire Department.

  • ! (NOT), which is useful if we want a filter for observations where the individual works for any department except the Public Works.

It is important to note that we can also group sets of conditions by using parentheses (). For example, we can specify the following filter to give me all individuals making 100000 or more except those working for either the Police Department OR Fire Department:

salaries %>%
  filter(gross_wages >= 100000 & !(department == "Police Department" | department == "Fire Department"))
last_name first_name gross_wages department
ALDRICH SONIA 132251.4 Accounting
BOCKELMAN PAUL 193575.7 Town Manager’s Office
BRESTRUP CHRISTINE 100296.8 Planning
HANNON SEAN 114731.7 Information Technology
MOORING GUILFORD 143171.3 Public Works
MORRA ROBERT 115846.7 Insepection Services
RUSIECKI AMY 105054.2 Public Works
SHARRY SHARON 111600.5 Jones Library
ZIOMEK MATTHEW 146471.4 Conservation

Similarly, here is a filter that outputs the individuals who work for either the Public Works department OR the Town Clerk's office AND make more than 75000:

salaries %>%
  filter((department == "Public Works" | department == "Town Clerk's") & gross_wages > 75000)
last_name first_name gross_wages department
AUDETTE SUSAN 75618.29 Town Clerk’s
BURROWS GLENN 78790.40 Public Works
CALL STEPHEN 93817.18 Public Works
COOMBS RUSSELL 77628.58 Public Works
DETHIER PAUL 92711.90 Public Works
JORDAN JAMES 80683.27 Public Works
KLIMCZYK DUANE 97872.68 Public Works
MERCIER JOSEPH 84977.56 Public Works
MOORING GUILFORD 143171.29 Public Works
RUSIECKI AMY 105054.22 Public Works
SKEELS JASON 99653.93 Public Works
SNOW ALAN 87216.27 Public Works
TELEGA STEVE 75281.08 Public Works
WILLIS RODNEY 82957.78 Public Works
WILLSON ELIZABETH 77487.88 Public Works
WOYNAR MARK 78980.26 Public Works

As you can see, 16 individuals matched my filter criteria.

count()

We use the count() function whenever we want to count how often a unique value comes up inside a variable. This is useful whenever we want to see how many of something we have in our dataset.

For example, if we want to see how many people there are in each department (i.e., how often each unique value appears within the department variable, since each observation in our dataset refers to a different person), we can just supply the variable we want to count by as the lone argument.

salaries %>%
  count(department)
department n
Accounting 6
Amherst Recreation 94
Animal Control 1
Assessor 4
Cherry Hill Golf Course 2
Collectors 6
Conservation 8
Dispatch 15
Facilites/Maintenance 10
Fire Department 87
Information Technology 8
Insepection Services 12
Jones Library 57
Parking Enforcement 2
Planning 6
Police Department 86
Public Health 6
Public Works 82
Senior Center 34
Town Clerk’s 247
Town Council 21
Town Manager’s Office 8

Notice that this created a new data frame for us that contains just two variables (department and n, or the number of times that unique value came up within the variable). Put another way, we can’t get information for the first_name variable anymore because it’s gone. This operation has thus altered our data for subsequent steps. (Of course, because we did not re-assign it back to the original salaries object, we can always go back to the original data in a separate operation.)

As we can see, the “Animal Control” department had the fewest employees in our dataset.

Do note that you can actually supply multiple variables to count by, simply by providing them as additional arguments. For example, if we want all of the unique last_name values within all of the unique department values, we would simply write:

salaries %>%
  count(department, last_name)
department last_name n
Accounting ALDRICH 1
Accounting BOWSER 1
Accounting DELANEY 1
Accounting FLEURENT 1
Accounting LITTMANN 1
Accounting MANGANO 1
Amherst Recreation ABDEL-MAKSOUD 1
Amherst Recreation ABELLI 1
Amherst Recreation BENSON 1
Amherst Recreation BETE 1
Amherst Recreation BILZ 1
Amherst Recreation BORRELL 1
Amherst Recreation BRAININ 1
Amherst Recreation BRAITHWAITE 1
Amherst Recreation BROWN 1
Amherst Recreation BURKE 1
Amherst Recreation CALI 1
Amherst Recreation CESAR-BONE 1
Amherst Recreation CITINO 1
Amherst Recreation CLOUGH 1
Amherst Recreation COELHO 1
Amherst Recreation CONNOLLY 1
Amherst Recreation DESAUTELS 1
Amherst Recreation DOHERTY 1
Amherst Recreation FARNHAM 2
Amherst Recreation FERNANDEZ-SIERRA 1
Amherst Recreation FORDHAM 1
Amherst Recreation GAGNON 1
Amherst Recreation GEFFERT 1
Amherst Recreation GEOGHEGAN 1
Amherst Recreation GOMBA 1
Amherst Recreation GONNETT 1
Amherst Recreation GOODWIN 1
Amherst Recreation HASSAN 1
Amherst Recreation HOLDEN 1
Amherst Recreation HOPKINS MCGILL 1
Amherst Recreation IRANI 2
Amherst Recreation JEAN-PHILIPPE 1
Amherst Recreation JORDAN 1
Amherst Recreation KABANGU 1
Amherst Recreation KATZ 1
Amherst Recreation KEENAN 1
Amherst Recreation KLAES 2
Amherst Recreation KRAMER 1
Amherst Recreation KUPPERMAN 1
Amherst Recreation LAMB 1
Amherst Recreation LARSON 1
Amherst Recreation LEONARD 1
Amherst Recreation LIBONATE 1
Amherst Recreation MACPHETRES 1
Amherst Recreation MALONEY 1
Amherst Recreation MARCZUK 1
Amherst Recreation MCCUTCHEON 1
Amherst Recreation MCKENZIE 1
Amherst Recreation MILES 1
Amherst Recreation MORALES-FERNANDEZ 1
Amherst Recreation MOTT 1
Amherst Recreation MOYSTON 1
Amherst Recreation MULVEY 1
Amherst Recreation MURPHY 1
Amherst Recreation NADEAU 1
Amherst Recreation NADOLSKI 1
Amherst Recreation NDIKUMANA 1
Amherst Recreation NORMAN 1
Amherst Recreation O’LOUGHLIN 1
Amherst Recreation O’SULLIVAN 1
Amherst Recreation PASTORELLO 1
Amherst Recreation PENNIMAN 1
Amherst Recreation PHILIBERT 1
Amherst Recreation PODSIADLO 1
Amherst Recreation POLINO 1
Amherst Recreation RANDAZZO 1
Amherst Recreation RIETKIRK 1
Amherst Recreation ROSA 1
Amherst Recreation ROSS-PERKINS 1
Amherst Recreation SAUNDERS 1
Amherst Recreation SAYEGH 1
Amherst Recreation SCHILLING 3
Amherst Recreation SHERWOOD 2
Amherst Recreation SKELTON 1
Amherst Recreation SOSA 1
Amherst Recreation STIVEN 1
Amherst Recreation SUAREZ 1
Amherst Recreation SULLIVAN 1
Amherst Recreation THIBAULT 2
Amherst Recreation TILUS 1
Amherst Recreation VILLANUEVA 1
Amherst Recreation WALAS 2
Amherst Recreation WEILERSTEIN 1
Amherst Recreation WOODS 1
Amherst Recreation YOUNG 2
Animal Control HEPBURN 1
Assessor BOUTHILETTE SARNA 1
Assessor BURGESS 1
Assessor DUFFY 1
Assessor TURATI 1
Cherry Hill Golf Course SMITH 1
Cherry Hill Golf Course WEBER 1
Collectors BOUCHER 1
Collectors CARY 1
Collectors GOODHIND 1
Collectors JOHNSON 1
Collectors KACEY 1
Collectors LAFOUNTAIN 1
Conservation BORDEWIECK 1
Conservation CICCARELLO 1
Conservation DATTILO 1
Conservation JACQUE 1
Conservation LASS 1
Conservation PEASE 1
Conservation WILLIAMS 1
Conservation ZIOMEK 1
Dispatch ANTHONY 1
Dispatch ASCHER 1
Dispatch BARRY 1
Dispatch CHUDZIK 1
Dispatch CICIA 1
Dispatch CURTIN 1
Dispatch EVERETT 1
Dispatch LYNCH 1
Dispatch OLES 1
Dispatch PRONOVOST 1
Dispatch RIVERA 1
Dispatch RUSHFORD 1
Dispatch SWEET 1
Dispatch TORRES 1
Dispatch WORTHLEY 1
Facilites/Maintenance CLEVELAND 1
Facilites/Maintenance CORMIER 1
Facilites/Maintenance DECKER 1
Facilites/Maintenance FLEURENT 1
Facilites/Maintenance HATHAWAY 1
Facilites/Maintenance IMBIMBO 1
Facilites/Maintenance LAPLANTE 1
Facilites/Maintenance LATVALLA 1
Facilites/Maintenance LEDUC 1
Facilites/Maintenance SHULAR 1
Fire Department AARONSON 1
Fire Department ALDRICH 1
Fire Department ARBOUR 2
Fire Department AUSTIN 1
Fire Department AVERILL 1
Fire Department BAKER 1
Fire Department BASCOMB 1
Fire Department BERGERON 1
Fire Department BORKHUIS 1
Fire Department BURGESS 1
Fire Department CHANDLER 1
Fire Department CHIPMAN 1
Fire Department CHIREKOS 1
Fire Department CILIBERTO 1
Fire Department CLAIRE 1
Fire Department CLOONEY 1
Fire Department COFFEY 1
Fire Department CORMIER 1
Fire Department COTE 1
Fire Department CUTRONE 1
Fire Department DANGER 1
Fire Department DION 1
Fire Department DOYLE 1
Fire Department EVE-COWLES 1
Fire Department FLAHERTY 1
Fire Department FLYNN 1
Fire Department FORSAITH 1
Fire Department GAUGHAN 1
Fire Department GESSEN 1
Fire Department GILLISPIE 1
Fire Department GOODHIND 1
Fire Department GORDON 1
Fire Department GORMAN 1
Fire Department GRAHAM 1
Fire Department GREENFIELD 1
Fire Department GWYTHER 1
Fire Department HOLMES 1
Fire Department INGRAM 1
Fire Department KASAL 1
Fire Department KELLY 1
Fire Department KELSEN 1
Fire Department KENNEDY 1
Fire Department KNYT 1
Fire Department LABANC 1
Fire Department LADD 1
Fire Department LAGASSE 1
Fire Department LEWIS 1
Fire Department LINDBERG 1
Fire Department LITALIEN 1
Fire Department LOFTUS 1
Fire Department MANEY 1
Fire Department MARQUIS 1
Fire Department MARTELL 1
Fire Department MCCOMB 1
Fire Department MELENDEZ 1
Fire Department MILLER 1
Fire Department MINER 1
Fire Department MORRIS 1
Fire Department NELSON 1
Fire Department OLMSTEAD 1
Fire Department PARR 1
Fire Department PELLETIER 1
Fire Department ROE 1
Fire Department ROSA 1
Fire Department ROSENBLATT 1
Fire Department ROY 1
Fire Department RYCZEK 1
Fire Department SAWICKI 1
Fire Department SCARDINO 1
Fire Department SINGH 1
Fire Department SMITH 2
Fire Department SPOSITO 1
Fire Department STEININGER 1
Fire Department STROMGREN 1
Fire Department SZEWCZYNSKI 1
Fire Department THEILMAN 1
Fire Department THOMSEN 1
Fire Department TUNNELL 1
Fire Department VALLE 1
Fire Department VALOVCIN 1
Fire Department VECCHIO 1
Fire Department VENEY 1
Fire Department VISNIEWSKI 1
Fire Department WILKINSON 1
Fire Department WILLIAMS 1
Information Technology DUDKIEWICZ 1
Information Technology FEDOROVSKY 1
Information Technology GLOVER 1
Information Technology HANNON 1
Information Technology SALTIS 1
Information Technology SUNRYD 1
Information Technology VOCCA 1
Information Technology WARNER 1
Insepection Services ANDERSON 1
Insepection Services CODY 1
Insepection Services MALONE 1
Insepection Services MCCARTHY 1
Insepection Services MORRA 1
Insepection Services O’BRIEN 1
Insepection Services RICKER-HORTON 1
Insepection Services SCHRYBA 1
Insepection Services SHEN 1
Insepection Services SMITH 1
Insepection Services THOMPSON 1
Insepection Services WASKIEWICZ 1
Jones Library ALLAN 1
Jones Library ANAYA 1
Jones Library ATTERIDGE 1
Jones Library BERUBE 1
Jones Library BOICE 1
Jones Library BOYD 1
Jones Library CABANA 1
Jones Library CASSIDY 1
Jones Library CASTANO-LONGEY 1
Jones Library COOKE 1
Jones Library CRANE 1
Jones Library DEMAREST 1
Jones Library DESABRAIS 1
Jones Library DOYAMA 1
Jones Library FAIR 1
Jones Library GADDY 1
Jones Library GRAY-MULLEN 1
Jones Library HARBESON 1
Jones Library HICKS-RICHARDS 1
Jones Library HUGUS 1
Jones Library HURWITZ 1
Jones Library JEZEK 1
Jones Library KING 1
Jones Library LAMSON 1
Jones Library LEVINE 1
Jones Library LO 1
Jones Library MARSZALEK 1
Jones Library MATTHEWS 1
Jones Library MCDONALD 1
Jones Library MCGLYNN 1
Jones Library MCLAUGHLIN 1
Jones Library NUESSLEIN 2
Jones Library OTERO 1
Jones Library PALMER 1
Jones Library PAREDES 1
Jones Library PLATT 1
Jones Library PYFROM 1
Jones Library ROTHBERG 1
Jones Library RYAN 1
Jones Library SHANNON 1
Jones Library SHARRY 1
Jones Library SLOCUM 1
Jones Library SPIEGEL 1
Jones Library STEINBERG 1
Jones Library STEPANEK 1
Jones Library STOKES 1
Jones Library STROUD 1
Jones Library VERTS 1
Jones Library VIGNONE 1
Jones Library WEIDENSAUL 1
Jones Library WEINTRAUB 1
Jones Library WENTWORTH 1
Jones Library WHITCOMB 1
Jones Library WHITE 1
Jones Library WINKLER 1
Jones Library ZENG 1
Parking Enforcement SARNA 1
Parking Enforcement ZHOU 1
Planning BREGER 1
Planning BRESTRUP 1
Planning FIELD-SADLER 1
Planning MALLOY 1
Planning MULLINS 1
Planning POLLOCK 1
Police Department AROCHO 1
Police Department BARONE 1
Police Department BLOW 1
Police Department BOSCHER 1
Police Department BUCEY 1
Police Department BULLOUGH 1
Police Department CANNEY 1
Police Department CARKHUFF 1
Police Department CARR 1
Police Department CARROLL 1
Police Department CHANDLER 1
Police Department CHUDZIK 1
Police Department CLARK 1
Police Department CORSETTI 1
Police Department CRUMP 1
Police Department CURLEY 1
Police Department DALY 1
Police Department DAMON 1
Police Department DAMOURAS 1
Police Department DIAZ-GUTIERREZ 1
Police Department DOUGLAS 1
Police Department DOWNIE 1
Police Department EDELL 1
Police Department ELLIOTT 1
Police Department FARBER 1
Police Department FELICIANO 2
Police Department FRYDRYK 1
Police Department GALLAGHER 1
Police Department GEARY 1
Police Department GERSTNER 1
Police Department GUINEE 1
Police Department HARRIS 1
Police Department HULSE 1
Police Department HUMBER 1
Police Department JOHNSON 1
Police Department JOLIE 1
Police Department KEYES 1
Police Department KNIGHTLY 2
Police Department KUC 1
Police Department LAMOUREUX 1
Police Department LANG 1
Police Department LAPLANTE 1
Police Department LARAMEE 1
Police Department LIVINGSTONE 1
Police Department LOPEZ 1
Police Department MACDONALD 1
Police Department MACLEAN 1
Police Department MARTIN-GRAHAM 1
Police Department MARTINS 1
Police Department MASPO 1
Police Department MATUSZKO 1
Police Department MENARD 2
Police Department METZGER 1
Police Department MONGEON 1
Police Department NAGLE 1
Police Department NEWCOMB 1
Police Department NEWELL 1
Police Department NEWMAN 1
Police Department PETERMAN 1
Police Department RAMIREZ FRANCO 1
Police Department REARDON 1
Police Department REILLY 1
Police Department SARNACKI 1
Police Department SATKOWSKI 1
Police Department SLOVIN 1
Police Department SOBIESKI 1
Police Department SORMANTI 1
Police Department SOVERINO 1
Police Department STEWART 1
Police Department SULLIVAN 1
Police Department SWIFT 1
Police Department THURSTON 1
Police Department TING 1
Police Department WATSON 1
Police Department WELCH 1
Police Department WHEELER 1
Police Department WISE 1
Police Department WOODS 1
Police Department WORTHLEY 1
Police Department WRIGHT 2
Police Department YOUNG 1
Police Department ZIOMEK 1
Public Health BROWN 1
Public Health DRAGON 1
Public Health FEDERMAN 1
Public Health KATZ 1
Public Health OBRIEN 1
Public Health SCHROEDER 1
Public Works BARDWELL 1
Public Works BARTON 1
Public Works BELANGER 1
Public Works BERGERON 1
Public Works BERGMANN 1
Public Works BERNARD 1
Public Works BOOM 1
Public Works BOWSER 1
Public Works BRACE 1
Public Works BURKE 1
Public Works BURROWS 1
Public Works CALL 1
Public Works CARAWAY 1
Public Works CARLSON 1
Public Works CLARK 2
Public Works COLBY 1
Public Works COOMBS 1
Public Works COTE 1
Public Works CROWLEY 1
Public Works CRUTCH 1
Public Works DAVIES 1
Public Works DAVIS 1
Public Works DERES 1
Public Works DETHIER 1
Public Works FELTMAN 1
Public Works FELTOVIC 1
Public Works FORBES 1
Public Works FOSTER 1
Public Works FROST 1
Public Works GANCORZ 1
Public Works GAUTHIER 1
Public Works GAVRON 1
Public Works GORMELY 1
Public Works GRANT 1
Public Works HAGAR 1
Public Works HENRY 1
Public Works HILLMEYER 1
Public Works ISABELLE 1
Public Works JACQUE 1
Public Works JORDAN 1
Public Works KLIMCZYK 1
Public Works KNIGHTLY 1
Public Works KNOLLE 1
Public Works LOFLAND 1
Public Works LONGTO 1
Public Works MERCIER 1
Public Works MITCHELL 1
Public Works MOORING 1
Public Works MURPHY 1
Public Works OLANYK 1
Public Works ORRELL 1
Public Works OUSLEY 1
Public Works PEREZ 1
Public Works PICKERING 1
Public Works PRICE 1
Public Works RUSIECKI 1
Public Works SANTIAGO 1
Public Works SEVIGNE 1
Public Works SKEELS 2
Public Works SKRIBISKI 1
Public Works SNOW 1
Public Works STAFFORD 1
Public Works SZWED 1
Public Works TAYLOR 1
Public Works TELEGA 1
Public Works THOMPSON 1
Public Works TIDLUND 1
Public Works TORO-RODRIGUEZ 1
Public Works VANNOY 1
Public Works VECCHIO 1
Public Works WATERMAN 1
Public Works WILLARD 1
Public Works WILLIS 2
Public Works WILLSON 1
Public Works WOYNAR 1
Public Works WRIGHT 1
Public Works YATES 1
Public Works YODER 1
Public Works ZAKAITIS 1
Senior Center BASCOMB 1
Senior Center BIRKHOLZ 1
Senior Center BURACK 1
Senior Center CHMURA 1
Senior Center DONOHUE 1
Senior Center GEORGAKAS 1
Senior Center HANCOCK 1
Senior Center HART 1
Senior Center HATCH 1
Senior Center HERMAN 2
Senior Center KOWLES 1
Senior Center KRANICK-WEINBERG 1
Senior Center KURTULUS 1
Senior Center LISSECK 2
Senior Center MACMELLON 1
Senior Center MAHMOODI 1
Senior Center MORTON 2
Senior Center OGULEWICZ 1
Senior Center PARKER-RILEY 1
Senior Center PHILLIPS 1
Senior Center RANEN 1
Senior Center RANSFORD 1
Senior Center RECTOR 1
Senior Center REYNOLDS 1
Senior Center ROSSI 1
Senior Center SALTZ 1
Senior Center SHAUGHAN 1
Senior Center STEVENS 1
Senior Center SZE 1
Senior Center VANDAL 1
Senior Center WHITNEY 1
Town Clerk’s ABRAMSON 1
Town Clerk’s AHO 1
Town Clerk’s ALVAREZ 1
Town Clerk’s APPEL 1
Town Clerk’s ASEBROOK 1
Town Clerk’s ASHBY 1
Town Clerk’s ASHTON 1
Town Clerk’s AUDETTE 2
Town Clerk’s AXELSON-BERRY 1
Town Clerk’s BAIR 1
Town Clerk’s BALLANTINE 2
Town Clerk’s BARBOZA 1
Town Clerk’s BARHAM 1
Town Clerk’s BASS 1
Town Clerk’s BATTY 1
Town Clerk’s BEAURY 1
Town Clerk’s BERGER 2
Town Clerk’s BERTRAND 1
Town Clerk’s BERWALD 1
Town Clerk’s BEYER 2
Town Clerk’s BIRTWISTLE 1
Town Clerk’s BLACK 1
Town Clerk’s BLAUSTEIN 2
Town Clerk’s BRACCIOTTI 1
Town Clerk’s BRADLEY 1
Town Clerk’s BREVIK 1
Town Clerk’s BROOKS 1
Town Clerk’s BRYAN 1
Town Clerk’s BRYANT 1
Town Clerk’s BURACK 1
Town Clerk’s CAIVANO 1
Town Clerk’s CAMPBELL 1
Town Clerk’s CANTOR 1
Town Clerk’s CHAPMAN 1
Town Clerk’s CHILDRESS 1
Town Clerk’s COLLINS 1
Town Clerk’s CONNER 1
Town Clerk’s CONOVER 1
Town Clerk’s CRAFTS-FINCH 1
Town Clerk’s CRAIG 1
Town Clerk’s CROWNER 1
Town Clerk’s DAPPRICH 1
Town Clerk’s DEDISCHEW 1
Town Clerk’s DIAZ 1
Town Clerk’s DOHRMANN 1
Town Clerk’s DOYLE 1
Town Clerk’s ENGELSON 2
Town Clerk’s ENRIGHT 1
Town Clerk’s EPSTEIN 1
Town Clerk’s FALSETTI-YU 1
Town Clerk’s FARNHAM 1
Town Clerk’s FASIHUDDIN 1
Town Clerk’s FINDLAY 1
Town Clerk’s FISCHER 1
Town Clerk’s FORD 1
Town Clerk’s FRADKIN 1
Town Clerk’s FRETWELL 1
Town Clerk’s FRIEDLANDER 1
Town Clerk’s FRONHOFER 1
Town Clerk’s FULLER 1
Town Clerk’s GALLANT 1
Town Clerk’s GAUTHIER 1
Town Clerk’s GEORGE 1
Town Clerk’s GINSBERG PELTZ 1
Town Clerk’s GLADSTONE-GILBERT 1
Town Clerk’s GLOCHESKI 1
Town Clerk’s GOODWIN 1
Town Clerk’s GOREN 1
Town Clerk’s GRABEL 1
Town Clerk’s GRANT 1
Town Clerk’s GRDEN 1
Town Clerk’s GRIFFIN 1
Town Clerk’s GRISWOLD 1
Town Clerk’s GROSS 1
Town Clerk’s HAMILTON 1
Town Clerk’s HAMMANN 1
Town Clerk’s HARRAGHY 1
Town Clerk’s HAYDEN 1
Town Clerk’s HERBERT 1
Town Clerk’s HIRSHBERG 1
Town Clerk’s HLAVA 1
Town Clerk’s HOCKEN 1
Town Clerk’s HOFFMAN 1
Town Clerk’s HOFFMANN 1
Town Clerk’s HORRIGAN 1
Town Clerk’s HUBBELL 1
Town Clerk’s JAFFEE 1
Town Clerk’s JENAL 1
Town Clerk’s JOHNSON 2
Town Clerk’s KEENAN 1
Town Clerk’s KELLEY 1
Town Clerk’s KELLMAN 1
Town Clerk’s KELLY 1
Town Clerk’s KING 2
Town Clerk’s KOLENIK 1
Town Clerk’s KROGH 1
Town Clerk’s KUDATSKY 1
Town Clerk’s LABICH 1
Town Clerk’s LANGDON 1
Town Clerk’s LARUDEE 1
Town Clerk’s LAUS 1
Town Clerk’s LAXSON 1
Town Clerk’s LEDERMAN 1
Town Clerk’s LEDOUX 1
Town Clerk’s LEFLAR 1
Town Clerk’s LEHRER 1
Town Clerk’s LENBURG 1
Town Clerk’s LHAMO 1
Town Clerk’s LINDSEY 1
Town Clerk’s LOEB 1
Town Clerk’s LONGTO 1
Town Clerk’s LOWERY 1
Town Clerk’s LUGO 1
Town Clerk’s LYNCH 1
Town Clerk’s MABEE 1
Town Clerk’s MAGARIAN 1
Town Clerk’s MAIDANA 1
Town Clerk’s MAINZER 2
Town Clerk’s MAIRS 1
Town Clerk’s MANNING 1
Town Clerk’s MARKHAM 2
Town Clerk’s MARQUARDT 1
Town Clerk’s MARSHALL 1
Town Clerk’s MARTIN 2
Town Clerk’s MASCIS 1
Town Clerk’s MATHEWS 1
Town Clerk’s MAWSON 1
Town Clerk’s MCLAUGHLIN 1
Town Clerk’s MCPEAK-LAROCCA 1
Town Clerk’s MEADE 1
Town Clerk’s MEAGHER 1
Town Clerk’s MELBIN 1
Town Clerk’s MERRIAM 1
Town Clerk’s MITCHELL 1
Town Clerk’s MONE 1
Town Clerk’s MOORE 1
Town Clerk’s MORGAN 1
Town Clerk’s MORSE 1
Town Clerk’s MOULTON 1
Town Clerk’s MURRAY 1
Town Clerk’s NEALE 1
Town Clerk’s NEUBAUER 1
Town Clerk’s NEWMAN 2
Town Clerk’s NORDEN 1
Town Clerk’s NUSSBAUM 1
Town Clerk’s O’BRIEN 1
Town Clerk’s O’CONNOR 1
Town Clerk’s O’MEARA 1
Town Clerk’s OLANYK 1
Town Clerk’s ORAM 1
Town Clerk’s OVERBY 1
Town Clerk’s OWENS 1
Town Clerk’s PARKER-RILEY 1
Town Clerk’s PARKS 1
Town Clerk’s PAVLOVA-GILLHAM 1
Town Clerk’s PAYAN 1
Town Clerk’s PETERS 1
Town Clerk’s PEWTHERER 1
Town Clerk’s PISTRANG 2
Town Clerk’s RANSFORD 1
Town Clerk’s RAWLINGS 1
Town Clerk’s REARDON 1
Town Clerk’s RECTOR 1
Town Clerk’s REID 1
Town Clerk’s RENTSCH 1
Town Clerk’s REPETTI 1
Town Clerk’s RESNICK 1
Town Clerk’s RHODEWALT 1
Town Clerk’s RICE 1
Town Clerk’s ROBINSON 1
Town Clerk’s ROMANELLI 1
Town Clerk’s ROSSI 1
Town Clerk’s ROWELL 1
Town Clerk’s ROZNOY 1
Town Clerk’s RUESCHEMEYER-BAILEY 1
Town Clerk’s SAMMIS 1
Town Clerk’s SANTIAGO 1
Town Clerk’s SARRO 1
Town Clerk’s SETTY 1
Town Clerk’s SETTY-CHARITY 1
Town Clerk’s SHALLCROSS 2
Town Clerk’s SHARMA 1
Town Clerk’s SHAUGHAN 1
Town Clerk’s SHERIDAN 1
Town Clerk’s SHULMAN 1
Town Clerk’s SKOLFIELD 1
Town Clerk’s SMITH 2
Town Clerk’s SPIRO 1
Town Clerk’s SPRINGER 1
Town Clerk’s STAIR 1
Town Clerk’s STANFORTH 1
Town Clerk’s STEARNS 1
Town Clerk’s STEIN 1
Town Clerk’s STEVENS 2
Town Clerk’s STREETER 1
Town Clerk’s SULLIVAN 1
Town Clerk’s TAN 1
Town Clerk’s TAUNTON 1
Town Clerk’s TERRIZZI 1
Town Clerk’s THOMPSON 3
Town Clerk’s THOMSON 1
Town Clerk’s TICKNOR-SWANSON 1
Town Clerk’s TRIPP 1
Town Clerk’s TUTHILL 1
Town Clerk’s TYLER 1
Town Clerk’s URSCHEL 1
Town Clerk’s VALENTINE 1
Town Clerk’s VAN STEENBURGH 1
Town Clerk’s VANIN 1
Town Clerk’s VANTREESE 1
Town Clerk’s VREDENBURG 1
Town Clerk’s WEAVER 1
Town Clerk’s WEIDENFELD 1
Town Clerk’s WEINER 1
Town Clerk’s WEISBORD 1
Town Clerk’s WESTHEAD 1
Town Clerk’s WHITTEMORE 1
Town Clerk’s WILSON 1
Town Clerk’s WINER 1
Town Clerk’s WINTER 1
Town Clerk’s WIRTH 1
Town Clerk’s WITHERELL 1
Town Clerk’s WOODHULL 1
Town Clerk’s WRIGHT 1
Town Clerk’s WYSE 1
Town Clerk’s YELLE 1
Town Clerk’s ZAFIAN 1
Town Clerk’s ZHANG 1
Town Clerk’s ZUCKER 1
Town Council BREWER 1
Town Council DE ANGELIS 1
Town Council DEMLING 1
Town Council DUMONT 1
Town Council GRIESEMER 1
Town Council HANNEKE 1
Town Council LORD 1
Town Council MARTENEY 1
Town Council MCCONNELL 1
Town Council MCDONALD 1
Town Council MILNE 1
Town Council OKEEFFE 1
Town Council PAM 1
Town Council REARDON 1
Town Council ROSS 1
Town Council RYAN 1
Town Council SCHOEN 1
Town Council SCHREIBER 1
Town Council SPITZER 1
Town Council STEINBERG 1
Town Council SWARTZ 1
Town Manager’s Office BOCKELMAN 1
Town Manager’s Office KENNEALLY 1
Town Manager’s Office KRAVITZ 1
Town Manager’s Office MILLS 1
Town Manager’s Office MISIASZEK 1
Town Manager’s Office MOYSTON 1
Town Manager’s Office RIVERA-RIFFENBURG 1
Town Manager’s Office ZLOGAR 1

(In this case, very few departments have more than one employee sharing a last name. However, if you look closely at the full output on your computer, you will see that most do!)

arrange()

We use the arrange() function whenever we want to sort our data in some fashion. We pair it with a particular variable (what to sort by) by supplying the variable as an argument. We can also specify if we want it in ascending order (low-to-high, which is the default) or descending order (high-to-low, by using the desc() function within it).

To illustrate this, let’s add a third step to our earlier code counting the unique department values by supplying an additional pipe (%>%). In that third step, we will sort the resulting data frame (count of department) in descending order based on the n variable:

salaries %>%
  count(department) %>%
  arrange(desc(n))
department n
Town Clerk’s 247
Amherst Recreation 94
Fire Department 87
Police Department 86
Public Works 82
Jones Library 57
Senior Center 34
Town Council 21
Dispatch 15
Insepection Services 12
Facilites/Maintenance 10
Conservation 8
Information Technology 8
Town Manager’s Office 8
Accounting 6
Collectors 6
Planning 6
Public Health 6
Assessor 4
Cherry Hill Golf Course 2
Parking Enforcement 2
Animal Control 1

Again, remember that n was not in our original dataset but became a variable in the data frame created by the count() function, which is what we’re passing forward to the arrange() function because of how we’ve ordered our pipe.

As we can see, the “Town Clerk’s” department had the most employees in our dataset.

summarize()

We use summarize() whenever we want to take multiple values from a variable and return a single value summarizing some aspect of that variable. This includes measures of central tendency and dispersion for that variable, such as the minimum, maximum, mean, median, variance, or standard deviation.

We supply the summarize() function with an argument structured with (1) the name of the variable we want to create/compute (e.g., mean_wages), (2) followed by the equal sign (=) to denote what we want that variable to be equal to, which is followed by (3) the operation we want to perform to calculate the summary statistic (e.g., mean(gross_wages)).

This is what that would look like in practice:

salaries %>%
  summarize(mean_wages=mean(gross_wages))
mean_wages
28207.08

Notice how we now get a dataset with a single observation and a single variable. That is because we have calculated a single summary statistic for all of our observations. Put another way, the summarize() function aggregates our data by producing a new data frame with the new variables we want to calculate (e.g., aggregate by). As the dataset has been aggregated, we cannot go back to the original data within this operation.

If we want to calculate multiple summary statistics, we would just supply them as additional arguments to the summarize() function. For example, here is how we would calculate the mean, median, and maximum salaries in our dataset:

salaries %>%
  summarize(mean_wages=mean(gross_wages), median_wages=median(gross_wages), max_wages=max(gross_wages))
mean_wages median_wages max_wages
28207.08 2625.13 193575.7

group_by()

We use the group_by() function whenever we want to apply an operation(s) separately for each unique value within a variable. This is particularly useful when we want to treat groups independently.

With the group_by() function, we simply supply the grouping variable (the variable containing the unique values we want to apply operations independently to) as an argument.

For example, if we wanted to calculate the mean, median, and max values for the gross_wages variable for each department in our variable, we would make the department our grouping variable. However, we would need to specify our groupings before we calculate our summary statistics. This is how that would work out:

salaries %>%
  group_by(department) %>%
  summarize(mean_wages=mean(gross_wages), median_wages=median(gross_wages), max_wages=max(gross_wages))
department mean_wages median_wages max_wages
Accounting 87012.355 81985.005 132251.42
Amherst Recreation 7204.356 1801.010 99137.75
Animal Control 64764.610 64764.610 64764.61
Assessor 44990.363 42316.300 87894.23
Cherry Hill Golf Course 7773.855 7773.855 10933.22
Collectors 67738.212 66005.110 95341.52
Conservation 48584.656 43137.440 146471.42
Dispatch 56016.981 71450.580 89333.01
Facilites/Maintenance 42099.275 47229.480 63071.19
Fire Department 52918.321 62794.990 160049.71
Information Technology 68222.535 69733.145 114731.67
Insepection Services 67777.181 76650.645 115846.68
Jones Library 31355.121 14975.120 111600.51
Parking Enforcement 48469.060 48469.060 53869.70
Planning 71292.585 66812.925 100296.78
Police Department 59128.506 74225.325 172626.81
Public Health 29184.952 12169.205 75330.05
Public Works 52908.318 57070.480 143171.29
Senior Center 9690.516 1500.000 82973.77
Town Clerk’s 1021.992 112.710 75618.29
Town Council 6785.425 5000.040 63673.53
Town Manager’s Office 63523.214 56462.925 193575.69

Note that if we just run the first two lines (i.e., ommitting the summarize() line), we won’t see any apparent differences when compared to just running the first line. That’s because we’ve told R to add grouping information but have not yet done anything with it. The noticeable change only occurs when we perform an operation on those groups, which is what the third line is doing.

As with the count() variable, we can also nest our groupings (i.e., perform the operation separately on each last_name within each department) by supplying them as additional arguments to the group_by() function.

Additionally, it is worth noting here that the count() function is effectively just a wrapper (shortcut) for calculating a summary statistic. We could replicate our count() results from above by performing an operation with the n() function, like this:

salaries %>%
  group_by(department) %>%
  summarize(n=n())
department n
Accounting 6
Amherst Recreation 94
Animal Control 1
Assessor 4
Cherry Hill Golf Course 2
Collectors 6
Conservation 8
Dispatch 15
Facilites/Maintenance 10
Fire Department 87
Information Technology 8
Insepection Services 12
Jones Library 57
Parking Enforcement 2
Planning 6
Police Department 86
Public Health 6
Public Works 82
Senior Center 34
Town Clerk’s 247
Town Council 21
Town Manager’s Office 8

As this example helps illustrates, there are often several different ways accomplish the same thing in R. Some functions are more efficient (faster) than others. However, for now, just stick to the functions and approaches that make the most sense to you.

ggplot()

We use ggplot() and its child functions whenever we want to create visualizations of our datasets. This function comes from the powerful ggplot2 package (which is loaded by tidyverse).

ggplots use what we call a grammar of graphics, which is basically a general scheme for data visualization that breaks up the visual into semantic components such as scales and layers.

You can think of this layering process as being conceptually similar to the piping (%>%) we’ve done throughout this tutorial — it is just putting one thing on top of the other. However, unlike the pipes we’ve used, ggplot’s semantic uses the plus sign (+) at the end of a line of code to signal the addition of a layer in the subsequent line.

This often confuses users who are new to R, so it bears repeating here. When you’ve started building your ggplot, use + in places where you might otherwise feel tempted to use the piping operator (%>%). Under the hood, they’re very different things. However, conceptually, they’re not too dissimilar.

Let’s illustrate this by creating a bar chart with the mean wage for the 5 departments with the highest mean salaries.

We’ll start by creating the data frame we’ll use to plot:

salaries %>%
  group_by(department) %>%
  summarize(median_wages=median(gross_wages)) %>%
  arrange(desc(median_wages)) %>% %>%
  head(5)
department median_wages
Accounting 81985.01
Insepection Services 76650.65
Police Department 74225.32
Dispatch 71450.58
Information Technology 69733.15

The result that you see above is the information that we will be providing to our ggplot. Put another way, it is a manipulated (in this case, aggregated) slice of our original data.

The ggplot semantic is quite powerful, so we will start with a very basic example. With any ggplot, the first layer will include some information about how to map the data onto a coordinate plane (e.g., X and Y axes).

In our example, let’s go ahead and add our base layer by using the ggplot() function and setting the mapping attribute by using the aes() (aesthetic mappings) function. Within that aes() function, we will map the department variable onto the X axis and the mean_wages variable onto the Y axis. Here’s what that looks like now.

salaries %>%
  group_by(department) %>%
  summarize(median_wages=median(gross_wages)) %>%
  arrange(desc(median_wages)) %>% %>%
  head(5) %>%
  ggplot(aes(x=department, y=median_wages))

A seemingly empty ggplot

Notice that we have our five departments in the X axis and the correct range (lowest and highest values) of the salaries in the Y axis. However, for us to see the values for each department, we need to add a second layer containing some geometric shape that represent the data.

There are a lot of different geometric shapes you can add to your plot as additional layers (e.g., to create line charts, scatterplots, etc.). You can also re-label your axes and change their ranges by adding those functions as layers. You can also simply flip the bars to be horizontal by adding a coord_flip() layer.

For a simple vertical bar graph, we can use the geom_col() function. Let’s use that function by adding a second layer at the end of our code.

salaries %>%
  group_by(department) %>%
  summarize(median_wages=median(gross_wages)) %>%
  arrange(desc(median_wages)) %>% %>%
  head(5) %>%
  ggplot(aes(x=department, y=median_wages)) +
    geom_col()

A ggplot with columns

We could go on to style the visualization to look much nicer (including organizing the bars from high-to-low!), but simple visualizations like this will usually do the trick during the exploratory phase of data analysis.

Approaching a Question

When you start to answer the question you’ve posed the data, it is helpful to break up the question logically into multiple steps before you even write a single line of code.

For example, consider the question: Among the Police, Fire, and Public Works departments, which one had the highest median wage?

This question asks us to:

  • Include only certain kinds of observations (filter(), matching certain department criteria)

  • Apply an operation separately to each group (group_by(), using the department variable)

  • Calculate a summary statistic (summarize(), to get the median() value of the different gross_wages vectors)

  • Sort the information (arrange(), in descending (desc()) order by the median wage variable)

Now that we know the logic of the question, we can translate it into the code below.

salaries %>%
  filter(department == "Police Department" | department == "Fire Department" | department == "Public Works") %>%
  group_by(department) %>%
  summarize(median_wages=median(gross_wages)) %>%
  arrange(desc(median_wages))
department median_wages
Police Department 74225.32
Fire Department 62794.99
Public Works 57070.48

Do note that the order of operations really matters here. If we execute the summarize() line before group_by(), it won’t work because it will have calculated a summary statistic for the entire dataset — and not separate statistics for each group.

I strongly suggest executing your code every time you add a new line, just so you can see what each line is doing and where any errors are popping up.