Creating Functions

Functions make code easier to read and reduce errors. This assignment is about data frame functions: functions whose input is a data frame (like filter() and group_by()), as opposed to vector functions (like mean()). Every function follows the same framework from the base R cheat sheet:

function_name <- function(variable) {
  # body: do something
  return(new_variable)
}

The flights data frame from nycflights13 loads as a “promise”, so we use it once to bring it into the environment, then look at it with skim().

data(flights)
flights
## # A tibble: 336,776 × 19
##     year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##    <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
##  1  2013     1     1      517            515         2      830            819
##  2  2013     1     1      533            529         4      850            830
##  3  2013     1     1      542            540         2      923            850
##  4  2013     1     1      544            545        -1     1004           1022
##  5  2013     1     1      554            600        -6      812            837
##  6  2013     1     1      554            558        -4      740            728
##  7  2013     1     1      555            600        -5      913            854
##  8  2013     1     1      557            600        -3      709            723
##  9  2013     1     1      557            600        -3      838            846
## 10  2013     1     1      558            600        -2      753            745
## # ℹ 336,766 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## #   hour <dbl>, minute <dbl>, time_hour <dttm>
skim(flights)
Data summary
Name flights
Number of rows 336776
Number of columns 19
_______________________
Column type frequency:
character 4
numeric 14
POSIXct 1
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
carrier 0 1.00 2 2 0 16 0
tailnum 2512 0.99 5 6 0 4043 0
origin 0 1.00 3 3 0 3 0
dest 0 1.00 3 3 0 105 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1.00 2013.00 0.00 2013 2013 2013 2013 2013 ▁▁▇▁▁
month 0 1.00 6.55 3.41 1 4 7 10 12 ▇▆▆▆▇
day 0 1.00 15.71 8.77 1 8 16 23 31 ▇▇▇▇▆
dep_time 8255 0.98 1349.11 488.28 1 907 1401 1744 2400 ▁▇▆▇▃
sched_dep_time 0 1.00 1344.25 467.34 106 906 1359 1729 2359 ▁▇▇▇▃
dep_delay 8255 0.98 12.64 40.21 -43 -5 -2 11 1301 ▇▁▁▁▁
arr_time 8713 0.97 1502.05 533.26 1 1104 1535 1940 2400 ▁▃▇▇▇
sched_arr_time 0 1.00 1536.38 497.46 1 1124 1556 1945 2359 ▁▃▇▇▇
arr_delay 9430 0.97 6.90 44.63 -86 -17 -5 14 1272 ▇▁▁▁▁
flight 0 1.00 1971.92 1632.47 1 553 1496 3465 8500 ▇▃▃▁▁
air_time 9430 0.97 150.69 93.69 20 82 129 192 695 ▇▂▂▁▁
distance 0 1.00 1039.91 733.23 17 502 872 1389 4983 ▇▃▂▁▁
hour 0 1.00 13.18 4.66 1 9 13 17 23 ▁▇▇▇▅
minute 0 1.00 26.23 19.30 0 8 29 44 59 ▇▃▆▃▅

Variable type: POSIXct

skim_variable n_missing complete_rate min max median n_unique
time_hour 0 1 2013-01-01 05:00:00 2013-12-31 23:00:00 2013-07-03 10:00:00 6936

skim() shows 19 variables: 4 character, 14 numeric, and 1 date-time.

Example 1: Count Numeric Columns

The snippet

Start with working code for one data frame (flights). select(where(is.numeric)) keeps only numeric columns, and ncol() counts them.

flights %>%
  select(where(is.numeric))
## # A tibble: 336,776 × 14
##     year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##    <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
##  1  2013     1     1      517            515         2      830            819
##  2  2013     1     1      533            529         4      850            830
##  3  2013     1     1      542            540         2      923            850
##  4  2013     1     1      544            545        -1     1004           1022
##  5  2013     1     1      554            600        -6      812            837
##  6  2013     1     1      554            558        -4      740            728
##  7  2013     1     1      555            600        -5      913            854
##  8  2013     1     1      557            600        -3      709            723
##  9  2013     1     1      557            600        -3      838            846
## 10  2013     1     1      558            600        -2      753            745
## # ℹ 336,766 more rows
## # ℹ 6 more variables: arr_delay <dbl>, flight <int>, air_time <dbl>,
## #   distance <dbl>, hour <dbl>, minute <dbl>
flights %>%
  select(where(is.numeric)) %>%
  ncol()
## [1] 14

Saving the result under a name prints nothing, so we print the object on its own line:

ncol_num <- flights %>%
  select(where(is.numeric)) %>%
  ncol()

ncol_num
## [1] 14

Turn the snippet into a function

What changes? The data frame (flights). So the data becomes the argument .data. The function name is a verb that says what it does.

count_number_of_columns_numeric <- function(.data) {
  ncol_num <- .data %>%
    select(where(is.numeric)) %>%
    ncol()

  return(ncol_num)
}

Test it. flights has 14 numeric columns:

count_number_of_columns_numeric(flights)
## [1] 14

The first five columns (first ten rows) are all numeric:

flights[1:10, 1:5] %>%
  count_number_of_columns_numeric()
## [1] 5

Removing the first 13 columns leaves one character column (dest), four numeric columns, and one date-time column, so the answer is 4:

flights[1:10, -(1:13)]
## # A tibble: 10 × 6
##    dest  air_time distance  hour minute time_hour          
##    <chr>    <dbl>    <dbl> <dbl>  <dbl> <dttm>             
##  1 IAH        227     1400     5     15 2013-01-01 05:00:00
##  2 IAH        227     1416     5     29 2013-01-01 05:00:00
##  3 MIA        160     1089     5     40 2013-01-01 05:00:00
##  4 BQN        183     1576     5     45 2013-01-01 05:00:00
##  5 ATL        116      762     6      0 2013-01-01 06:00:00
##  6 ORD        150      719     5     58 2013-01-01 05:00:00
##  7 FLL        158     1065     6      0 2013-01-01 06:00:00
##  8 IAD         53      229     6      0 2013-01-01 06:00:00
##  9 MCO        140      944     6      0 2013-01-01 06:00:00
## 10 ORD        138      733     6      0 2013-01-01 06:00:00
flights[1:10, -(1:13)] %>%
  count_number_of_columns_numeric()
## [1] 4

Adding an argument for the details

The first argument supplies the data. A second argument can control the details of the operation. Here type_data chooses which type of column to count, with the default "numeric". An if statement picks the right body.

count_number_of_columns_type <- function(.data, type_data = "numeric") {
  if (type_data == "numeric") {
    ncol_type <- .data %>%
      select(where(is.numeric)) %>%
      ncol()
  } else if (type_data == "character") {
    ncol_type <- .data %>%
      select(where(is.character)) %>%
      ncol()
  } else {
    stop("type_data must be \"numeric\" or \"character\"")
  }

  return(ncol_type)
}
count_number_of_columns_type(flights)                           # default: numeric
## [1] 14
count_number_of_columns_type(flights, type_data = "character")  # override default
## [1] 4

The first five columns are all numeric, so there are zero character columns:

flights[1:10, 1:5] %>%
  count_number_of_columns_type(type_data = "character")
## [1] 0

Example 2: Count Rows

The snippet

Keep only the rows where carrier is "UA" (United Airlines), then count the rows with nrow(). filter() checks each row’s value in the carrier column and keeps the row if it is "UA".

flights %>%
  filter(carrier == "UA")
## # A tibble: 58,665 × 19
##     year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##    <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
##  1  2013     1     1      517            515         2      830            819
##  2  2013     1     1      533            529         4      850            830
##  3  2013     1     1      554            558        -4      740            728
##  4  2013     1     1      558            600        -2      924            917
##  5  2013     1     1      558            600        -2      923            937
##  6  2013     1     1      559            600        -1      854            902
##  7  2013     1     1      607            607         0      858            915
##  8  2013     1     1      611            600        11      945            931
##  9  2013     1     1      623            627        -4      933            932
## 10  2013     1     1      628            630        -2     1016            947
## # ℹ 58,655 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## #   hour <dbl>, minute <dbl>, time_hour <dttm>
flights %>%
  filter(carrier == "UA") %>%
  nrow()
## [1] 58665
nrow_ua <- flights %>%
  filter(carrier == "UA") %>%
  nrow()

nrow_ua
## [1] 58665

To count another carrier we change only the abbreviation:

flights %>% filter(carrier == "AA") %>% nrow()
## [1] 32729
flights %>% filter(carrier == "DL") %>% nrow()
## [1] 48110

Turn the snippet into a function

Two things change: the data frame and the carrier. So the function has two arguments: .data (the data) and carrier_name (the detail).

count_num_flights_by_carrier <- function(.data, carrier_name) {
  nrow_carrier <- .data %>%
    filter(carrier == carrier_name) %>%
    nrow()

  return(nrow_carrier)
}
count_num_flights_by_carrier(flights, "UA")
## [1] 58665
count_num_flights_by_carrier(flights, "AA")
## [1] 32729

A small test we can check by eye: in the first ten rows there are 3 United flights and 2 American flights.

flights[1:10, "carrier"]
## # A tibble: 10 × 1
##    carrier
##    <chr>  
##  1 UA     
##  2 UA     
##  3 AA     
##  4 B6     
##  5 DL     
##  6 UA     
##  7 B6     
##  8 EV     
##  9 B6     
## 10 AA
flights[1:10, ] %>% count_num_flights_by_carrier("UA")
## [1] 3
flights[1:10, ] %>% count_num_flights_by_carrier("AA")
## [1] 2

Example 3: Your Own Data

Replace the placeholders in the chunks below (marked TODO), then change eval=FALSE to eval=TRUE (or delete it) in each chunk.

Import your data

# TODO: replace with the file name of your data
my_data <- read_csv("your_data_file.csv")

my_data

The snippet

Choose a categorical variable in your data and one value in that variable. Use filter() and nrow() the same way as Example 2. (Need a refresher? Chapter 5, section 5.2, “Rows: filter()”.)

# TODO: replace category_column and "some_value" with your own column and value
my_data %>%
  filter(category_column == "some_value") %>%
  nrow()

The function

Turn the snippet into a function, just like Example 2. The data and the value are the two things that change, so they become the arguments.

# TODO: give the function a clear verb name and use your own column name
count_rows_by_category <- function(.data, category_value) {
  nrow_category <- .data %>%
    filter(category_column == category_value) %>%
    nrow()

  return(nrow_category)
}
# TODO: test with two different values from your column
count_rows_by_category(my_data, "some_value")
count_rows_by_category(my_data, "another_value")