Import your data

Create Data frame functions

Example 1: count columns

code snippets

ncol_num <- flights %>%
    
    # Select a type of variables
    select(where(is.numeric)) %>%
    
    # Count columns
    ncol()

ncol_num
## [1] 14

Turn them into a function

count_columns <- function(data) {

  data %>%
    select(where(is.numeric)) %>%
    ncol()
}

count_columns(flights)
## [1] 14

Adding arguments for details of operation

count_columns <- function(data, type) {

  data %>%
    select(where(type)) %>%
    ncol()
}

count_columns(flights, is.numeric)
## [1] 14

Example 2: count rows

code snippets

nrow_num <- flights %>%
    
    # filter rows that meet a condition
    filter(carrier == "UA") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 58665

Turn them into a function

count_rows <- function(data, carrier_name) {

  data %>%
    filter(carrier == carrier_name) %>%
    nrow()
}

count_rows(flights, "UA")
## [1] 58665

Example 3: count rows

Create your own.

code snippets

nrow_nyc <- flights %>%
  filter(origin == "JFK") %>%
  nrow()

nrow_nyc
## [1] 111279

Turn them into a function

count_origin <- function(data, airport) {

  data %>%
    filter(origin == airport) %>%
    nrow()
}

count_origin(flights, "JFK")
## [1] 111279