Import your data

sports <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-03-29/sports.csv')
## Rows: 132327 Columns: 28
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (8): institution_name, city_txt, state_cd, zip_text, classification_nam...
## dbl (20): year, unitid, classification_code, ef_male_count, ef_female_count,...
## 
## ℹ 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.
sports %>% skimr::skim()
Data summary
Name Piped data
Number of rows 132327
Number of columns 28
_______________________
Column type frequency:
character 8
numeric 20
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
institution_name 0 1.00 6 65 0 2212 0
city_txt 45 1.00 3 23 0 1376 0
state_cd 45 1.00 2 2 0 53 0
zip_text 45 1.00 5 9 0 3304 0
classification_name 0 1.00 4 34 0 19 0
classification_other 130642 0.01 3 50 0 110 0
sector_name 45 1.00 14 35 0 6 0
sports 0 1.00 4 26 0 38 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1.00 2017.68 1.49 2015 2016.00 2018.0 2019.0 2019 ▂▂▂▂▇
unitid 0 1.00 184360.44 56064.63 100654 149781.00 181738.0 214069.0 800001 ▇▁▁▁▁
classification_code 0 1.00 7.78 4.89 1 4.00 6.0 12.0 20 ▇▇▅▂▂
ef_male_count 0 1.00 2126.25 3014.38 0 513.00 986.0 2385.0 35954 ▇▁▁▁▁
ef_female_count 0 1.00 2496.21 3276.05 0 652.00 1248.0 2860.0 30325 ▇▁▁▁▁
ef_total_count 0 1.00 4622.46 6228.14 0 1194.00 2259.0 5237.0 66279 ▇▁▁▁▁
sector_cd 0 1.00 2.21 2.10 1 1.00 2.0 2.0 99 ▇▁▁▁▁
sportscode 0 1.00 16.34 10.57 1 7.00 16.0 25.0 38 ▇▅▆▅▃
partic_men 70462 0.47 30.86 29.36 1 13.00 22.0 35.0 331 ▇▁▁▁▁
partic_women 63442 0.52 20.71 18.91 1 11.00 16.0 23.0 327 ▇▁▁▁▁
partic_coed_men 131560 0.01 11.05 11.85 1 4.00 8.0 14.0 130 ▇▁▁▁▁
partic_coed_women 131560 0.01 14.17 13.84 1 5.00 10.0 19.0 91 ▇▂▁▁▁
sum_partic_men 0 1.00 14.49 25.30 0 0.00 0.0 20.0 331 ▇▁▁▁▁
sum_partic_women 0 1.00 10.86 17.15 0 0.00 6.0 17.0 327 ▇▁▁▁▁
rev_men 70462 0.47 809011.05 4885977.05 65 63428.00 158126.0 400604.0 156147208 ▇▁▁▁▁
rev_women 63444 0.52 279346.30 426320.88 0 58745.50 138318.0 331119.5 21440365 ▇▁▁▁▁
total_rev_menwomen 45193 0.66 795231.25 4180176.56 130 96299.00 228775.5 541875.5 156147208 ▇▁▁▁▁
exp_men 70462 0.47 662385.83 2678178.55 65 63062.00 159666.0 424025.0 69718059 ▇▁▁▁▁
exp_women 63442 0.52 331594.20 543368.55 65 59301.00 141800.0 361860.0 9485162 ▇▁▁▁▁
total_exp_menwomen 45191 0.66 732422.49 2422613.60 130 96435.75 234559.0 585603.5 69718059 ▇▁▁▁▁

Create Data frame functions

My Date 1: count numeric columns

code snippets

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

ncol_num
## [1] 20

Turn them into a function

count_numeric_col <- function(sports){
    
    # Body
    ncol_num <- sports %>%
        # Select a type of variables
        select(where(is.numeric)) %>%
    
        # Count columns
        ncol()
    
    # Return the new variable
    return(ncol_num)
    
}

sports %>% count_numeric_col()
## [1] 20
sports %>% .[1:10, 1:5] %>% count_numeric_col()
## [1] 2

Adding arguments for details of operation

count_ncol_type <- function(sports, type_data = "numeric"){
    
    # If statement for type of variables
    if(type_data == "numeric"){
        
        # Body
        ncol_type <- sports %>%
            # Select a type of variables
            select(where(is.numeric)) %>%
            
            # Count columns
            ncol()
        
    } else if(type_data == "character") {
        # Body
        ncol_type <- sports %>%
            
            # Select a type of variables
            select(where(is.character)) %>%
            
            # Count columns
            ncol()
    }
    
    # Return the new variable
    return(ncol_type)
}

sports %>% count_ncol_type()
## [1] 20
sports %>% count_ncol_type(type_data = "character")
## [1] 8
sports %>% .[1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 3

My Data 2: count rows

code snippets

nrow_num <- sports %>%
    
    # filter rows that meet a condition
    filter(sports == "Basketball") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 10000

Turn them into a function

count_num_schools_by_basketball <- function(sports, sports_name){
    
    # Body
    nrow_num <- sports %>%
    
        # filter rows that meet a condition
        filter(sports == "Basketball") %>%
    
        # Count rows
        nrow()
    
    # Return the new variable
    return(nrow_num)
}

sports %>% .[1:10, "sports"] %>%
count_num_schools_by_basketball(sports_name == "Basketball")
## [1] 1