Create Data Frame Functions
Example 1: Count Columns
ncol_num <- flights %>%
select(where(is.numeric)) %>%
ncol()
ncol_num
## [1] 14
Turn into a Function
count_ncol_numeric <- function(.data) {
ncol_num <- .data %>%
select(where(is.numeric)) %>%
ncol()
return(ncol_num)
}
flights %>% count_ncol_numeric()
## [1] 14
flights %>% .[1:10, -1:-13] %>% count_ncol_numeric()
## [1] 4
Adding Arguments for Details of Operation
count_ncol_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()
}
return(ncol_type)
}
flights %>% count_ncol_type()
## [1] 14
flights %>% count_ncol_type(type_data = "character")
## [1] 4
flights %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 0
Example 2: Count Rows
nrow_num <- flights %>%
filter(carrier == "UA") %>%
nrow()
nrow_num
## [1] 58665
Turn into a Function
count_num_flights_by_carrier <- function(.data, carrier_name) {
nrow_num <- .data %>%
filter(carrier == carrier_name) %>%
nrow()
return(nrow_num)
}
flights %>% count_num_flights_by_carrier(carrier_name = "UA")
## [1] 58665
flights %>% .[1:10, "carrier"] %>% count_num_flights_by_carrier(carrier_name = "AA")
## [1] 2
Example 3: Create Your Own Function
mydata <- read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-07-01/weekly_gas_prices.csv')
## Rows: 22360 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): fuel, grade, formulation
## dbl (1): price
## date (1): date
##
## ℹ 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.
nrow_regular_high <- mydata %>%
filter(grade == "regular") %>%
filter(price > 3.5) %>%
nrow()
nrow_regular_high
## [1] 699
Turn into a Function
count_num_regular_over_price <- function(.data, price_threshold = 3.5) {
nrow_regular_high <- .data %>%
filter(grade == "regular") %>%
filter(price > price_threshold) %>%
nrow()
return(nrow_regular_high)
}
mydata %>% count_num_regular_over_price()
## [1] 699
mydata %>% count_num_regular_over_price(price_threshold = 4)
## [1] 122