Import your data

# excel file
data <- read_excel("../00_data/my data q&a.xlsx")
data
## # A tibble: 269,732 × 15
##       id name     sex   age   height weight team  noc   games  year season city 
##    <dbl> <chr>    <chr> <chr> <chr>  <chr>  <chr> <chr> <chr> <dbl> <chr>  <chr>
##  1     1 A Dijia… M     24    180    80     China CHN   1992…  1992 Summer Barc…
##  2     2 A Lamusi M     23    170    60     China CHN   2012…  2012 Summer Lond…
##  3     3 Gunnar … M     24    NA     NA     Denm… DEN   1920…  1920 Summer Antw…
##  4     4 Edgar L… M     34    NA     NA     Denm… DEN   1900…  1900 Summer Paris
##  5     5 Christi… F     21    185    82     Neth… NED   1988…  1988 Winter Calg…
##  6     5 Christi… F     21    185    82     Neth… NED   1988…  1988 Winter Calg…
##  7     5 Christi… F     25    185    82     Neth… NED   1992…  1992 Winter Albe…
##  8     5 Christi… F     25    185    82     Neth… NED   1992…  1992 Winter Albe…
##  9     5 Christi… F     27    185    82     Neth… NED   1994…  1994 Winter Lill…
## 10     5 Christi… F     27    185    82     Neth… NED   1994…  1994 Winter Lill…
## # ℹ 269,722 more rows
## # ℹ 3 more variables: sport <chr>, event <chr>, medal <chr>

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_ncol_numeric <- function(.data) {

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

ncol_num

    return(ncol_num)
}
flights %>% count_ncol_numeric ()
## [1] 14
flights %>% .[1:10, -1:-13] %>% count_ncol_numeric ()
## [1] 14

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()
  } else {
    stop("Unsupported type. Use 'numeric' or 'character'.")
  }

  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 columns

code snippets

nrow_num <- flights %>%
    filter(carrier == "DL") %>%
    nrow()
nrow_num
## [1] 48110

Turn them into a function

count_num_flight_by_carrier <- function(.data, carrier_name) {
  nrow_num <- .data %>%
    filter(carrier == carrier_name) %>%
    nrow()
  
  return(nrow_num)
}

# Example usage
count_num_flight_by_carrier(flights, carrier_name = "UA")
## [1] 58665

Example 3: count rows

code snippets

ncol_num <- data %>%
  # Select only numeric columns
  select(where(is.numeric)) %>%
  # Count number of numeric columns
  ncol()

ncol_num
## [1] 2

Turn them into a function

count_num_people_by_age <- function(.data, age_value) {
  nrow_num <- .data %>%
    filter(age == age_value) %>%
    nrow()
  
  return(nrow_num)
}

# Example usage
count_num_people_by_age(data, age_value = 25)
## [1] 19699