Import your data

bee_colony <- read_excel("../00_data/MyData3.xlsx")

bee_colony %>% skimr::skim()
Data summary
Name Piped data
Number of rows 1222
Number of columns 10
_______________________
Column type frequency:
character 6
numeric 4
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
months 0 1 10 16 0 4 0
state 0 1 4 14 0 47 0
colony_max 0 1 2 7 0 279 0
colony_added 0 1 2 6 0 257 0
colony_reno 0 1 2 6 0 253 0
colony_reno_pct 0 1 1 2 0 55 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1.00 2017.77 1.89 2015 2016 2018 2019 2021 ▇▃▃▃▆
colony_size 47 0.96 123578.04 437835.18 1300 8000 17500 55500 3181180 ▇▁▁▁▁
colony_lost 47 0.96 16551.32 60544.42 20 950 2200 6500 502350 ▇▁▁▁▁
colony_lost_pct 54 0.96 11.38 7.23 1 6 10 15 52 ▇▅▁▁▁

Create Data frame functions

Count columns

code snippets

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

ncol_num
## [1] 4

Turn them into a function

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

bee_colony %>% count_ncol_numeric()
## [1] 4
bee_colony %>% . [1:10, 1:5]  %>% count_ncol_numeric()
## [1] 2

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 of function
         ncol_type <- .data %>% 
        
        # Select a type of variables
        select(where(is.numeric)) %>%
        
        # Count columns
        ncol()
    } else if (type_data == "character") {
       
        # Body of function
         ncol_type <- .data %>% 
        
        # Select a type of variables
        select(where(is.character)) %>%
        
        # Count columns
        ncol() 
    }
    
    
    # Return new variable 
    return(ncol_type)
    
} 

bee_colony %>% count_ncol_type()
## [1] 4
bee_colony %>% count_ncol_type(type_data = "character")
## [1] 6
bee_colony %>% . [1:10, 1:5] %>% count_ncol_type(type_data = "character")
## [1] 3

Count rows

code snippets

nrow_num <- bee_colony %>%
    
    # filter rows that meet a condition
    filter(state == "Massachusetts") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 26

Turn them into a function

count_num_colony_by_state <- function(.data, state_name) {
    
    # Body of function
        nrow_num <- .data %>%
        
        # filter rows that meet a condition
        filter(state == state_name) %>%
        
        # Count rows
        nrow()
    
    # Return the new variable
        return(nrow_num)
}

bee_colony %>% . [10:20, "state"] %>% count_num_colony_by_state(state_name = "Kansas")
## [1] 1