Import your data

data <- read_excel("../00_data/MyData.xlsx")

data %>% skimr::skim()
Data summary
Name Piped data
Number of rows 638
Number of columns 15
_______________________
Column type frequency:
character 4
numeric 11
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
team 0 1 5 13 0 32 0
team_name 0 1 4 10 0 32 0
playoffs 0 1 8 11 0 2 0
sb_winner 0 1 12 13 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1 2009.53 5.76 2000.0 2005.00 2010.0 2014.75 2019.0 ▇▇▇▇▇
wins 0 1 7.98 3.08 0.0 6.00 8.0 10.00 16.0 ▂▆▇▆▂
loss 0 1 7.98 3.08 0.0 6.00 8.0 10.00 16.0 ▂▆▇▆▂
points_for 0 1 350.28 71.40 161.0 299.00 348.0 396.00 606.0 ▂▇▇▂▁
points_against 0 1 350.28 59.55 165.0 310.00 347.0 391.50 517.0 ▁▃▇▆▁
points_differential 0 1 0.00 101.09 -261.0 -75.00 1.5 72.75 315.0 ▂▆▇▅▁
margin_of_victory 0 1 0.00 6.32 -16.3 -4.70 0.1 4.57 19.7 ▂▆▇▅▁
strength_of_schedule 0 1 0.00 1.63 -4.6 -1.10 0.0 1.20 4.3 ▁▅▇▅▁
simple_rating 0 1 0.00 6.20 -17.4 -4.47 0.0 4.50 20.1 ▁▆▇▅▁
offensive_ranking 0 1 0.00 4.34 -11.7 -3.18 0.0 2.70 15.9 ▁▇▇▂▁
defensive_ranking 0 1 0.00 3.57 -9.8 -2.40 0.1 2.50 9.8 ▁▅▇▅▁

Create Data frame functions

Example 1: count columns

code snippets

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

ncol_num
## [1] 11

Turn them into a function

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

data %>% count_ncol_numeric()
## [1] 11
data %>% .[1:10, -1:-10] %>% count_ncol_numeric()
## [1] 3

Adding arguments for details of operation

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

Example 2: count rows

code snippets

nrow_num <- data %>%
    
    # filter rows that meet a condition
    filter(team_name == "Patriots") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 20

Turn them into a function

count_num_teams_by_teamname <- function(.data, team_name) {
    #body
    nrow_num <- .data %>%
    
        # filter rows that meet a condition
        filter(team_name == "team_name") %>%
       
       # Count rows
       nrow()
    
    #return the new variable
    return(nrow_num)
}

 data %>% .[1:10, "team_name"] %>% count_num_teams_by_teamname(team_name = "Patriots")
## [1] 0

Example 3: count rows

code snippets

nrow_num <- data %>%
    
    # filter rows that meet a condition
    filter(team == "Miami") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 20

Turn them into a function

count_num_teams <- function(.data, team) {
    #body
    nrow_num <- .data %>% 
        
        #filter rows that meet condition 
        filter(team == "team") %>% 
        
        #count rows
        nrow()
    
    #return the new variable 
    return(nrow_num)
}

data %>% .[1:10, "team"] %>% count_num_teams(team = "Miami")
## [1] 0