Import your data

data <- readxl::read_xlsx("../00_data/Pokemon Data.xlsx") 

data %>% skimr::skim()
Data summary
Name Piped data
Number of rows 949
Number of columns 22
_______________________
Column type frequency:
character 10
numeric 12
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
pokemon 0 1 3 23 0 949 0
type_1 0 1 3 8 0 18 0
type_2 0 1 2 8 0 19 0
color_1 0 1 7 7 0 18 0
color_2 0 1 2 7 0 19 0
color_f 0 1 2 7 0 156 0
egg_group_1 0 1 3 13 0 15 0
egg_group_2 0 1 2 13 0 13 0
url_icon 0 1 2 55 0 722 0
url_image 0 1 83 85 0 949 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
id 0 1.00 1899.77 3507.95 1.0 238.0 475.0 712.0 10147.0 ▇▁▁▁▂
species_id 0 1.00 400.13 239.95 1.0 191.0 395.0 615.0 802.0 ▇▇▇▆▇
height 0 1.00 1.23 1.27 0.1 0.5 1.0 1.5 14.5 ▇▁▁▁▁
weight 0 1.00 66.21 119.95 0.1 8.5 28.8 66.6 999.9 ▇▁▁▁▁
base_experience 0 1.00 150.48 76.39 36.0 68.0 157.0 184.0 608.0 ▇▇▂▁▁
hp 0 1.00 68.95 25.93 1.0 50.0 65.0 80.0 255.0 ▃▇▁▁▁
attack 0 1.00 79.47 31.95 5.0 55.0 75.0 100.0 190.0 ▂▇▆▂▁
defense 0 1.00 74.07 30.76 5.0 50.0 70.0 90.0 230.0 ▃▇▃▁▁
special_attack 0 1.00 72.81 32.63 10.0 50.0 65.0 95.0 194.0 ▅▇▅▂▁
special_defense 0 1.00 72.22 27.68 20.0 50.0 70.0 90.0 230.0 ▇▇▂▁▁
speed 0 1.00 69.02 29.21 5.0 45.0 65.0 90.0 180.0 ▃▇▆▂▁
generation_id 147 0.85 3.69 1.93 1.0 2.0 4.0 5.0 7.0 ▇▅▃▅▅

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) {
    
        #Body    
        ncol_num <- .data %>%
        
        # Select a type of variables
        select(where(is.numeric)) %>%
        
        # Count columns
        ncol()
        
    return(ncol_num)
       
    
}


data %>% count_ncol_numeric()
## [1] 12
data %>% .[1:10, -1:-10] %>% count_ncol_numeric()
## [1] 5

Adding arguments for details of operation

count_ncol_type <- function(.data, type_of_data = "numeric") {
    
    if(type_of_data == "numeric") {
        
        #Body    
        ncol_num <- .data %>%
            
            # Select a type of variables
            select(where(is.numeric)) %>%
            
            # Count columns
            ncol()
            
    } else if(type_of_data == "character") {
           
        #Body    
        ncol_num <- .data %>%
            
            # Select a type of variables
            select(where(is.character)) %>%
            
            # Count columns
            ncol()
        
    }
        
        return(ncol_num)
       
    
}

data %>% count_ncol_type()
## [1] 12
data %>% count_ncol_type(type_of_data = "character")
## [1] 10
data %>% .[1:10, 1:5] %>% count_ncol_type(type_of_data = "character")
## [1] 1

Example 2: count rows

code snippets

nrow_num <- flights %>%
    
    # filter rows that meet a condition
    filter(carrier == "DL") %>%
    
    # Count rows
    nrow()

nrow_num
## [1] 48110

Turn them into a function

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

data %>% .[1:10, "type_1", drop = FALSE] %>%
  count_num_poke_type1(type_1 = "fire")
## [1] 10

Example 3: count rows

Create your own.

code snippets

Use the filter() function to select rows that meet a condition. Refer to Chapter 5.2 Filter rows with filter()

Turn them into a function