Intro to data transformations and summarizations

Nicky Wakim

dplyr basics

  • dplyr is a package in the tidyverse that provides a set of functions for data manipulation and transformation

 

  • Each function in dplyr is like a verb that describes the action being performed on the data

 

  • Most functions within dplyr have similar input and output:
    • Input: a data frame (usually the first argument in the function)
    • Output: a data frame
    • Additional arguments: typically describe what columns (variables) to operate on

Example of a dplyr function: select()

I can use the select() function to select specific columns (age_yr and ed) from my data frame (hrs_data)

hrs_data |>
  select(
    age_yr,
    ed
    )
1
The first argument is the data frame we want to operate on (in this case, hrs_data)
2
select is the function we are using on the data frame
3
age and ed are the columns (in the data frame) that we perform the select() function on
   age_yr ed
1      55 NA
2      56 12
3      55 NA
4      50 16
5      71 16
6      52 17
7      62 11
8      51 13
9      70 16
10     56 NA

Types of dplyr functions

dplyr functions can be categorized into four groups based on what they operate on:

  1. Rows: functions that operate on rows of a data frame
    • Examples: filter(), slice()
    • Under the Data Transformation lesson series
  2. Columns: functions that operate on columns of a data frame
    • Examples: select(), mutate()
    • Under the Data Transformation lesson series
  3. Groups: functions that operate on groups of rows in a data frame
    • Examples: group_by(), summarize()
    • Under the Data Summarization lesson series
  4. Joins: functions that combine two data frames based on a common key
    • Examples: left_join(), inner_join()
    • Not covered in this class

Resources