CH19 Functions

Introduction

Import Data

myData <- read_csv("../00_data/myData.csv")
## Rows: 1222 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): months, state
## dbl (8): year, colony_n, colony_max, colony_lost, colony_lost_pct, colony_ad...
## 
## ℹ 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.

Create data frame functions

Example 1 count columns

code snippets

ncol_num <- myData %>%
    
#select a type of variable 
  select(where(is.numeric)) %>%

#count columns 
   ncol()
   
ncol_num
## [1] 8

Turn them into a function

count_ncol_numeric <- function(.data) {
    
    #body
    ncol_num <- .data %>%
    
    #select a type of variable 
    select(where(is.numeric)) %>%

    #count columns 
     ncol()
    
   #return the new variable
    return(ncol_num)
    
}

myData %>% count_ncol_numeric()
## [1] 8
myData %>% .[1:10,-1:-5] %>% count_ncol_numeric()
## [1] 5

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 variable 
    select(where(is.numeric)) %>%

    #count columns 
     ncol()
    } else if(type_data == "character") {  
    
    #body
    ncol_type <- .data %>%
    
    #select a type of variable 
    select(where(is.numeric)) %>%
     #count columns 
     ncol()
    }
      #return the new variable
    return(ncol_num)
## [1] 8

Example 2 Count Rows

Code snippets

nrow_num <- myData %>%
  #Filter rows that meet a condition 
    filter(state == "Arizona") %>%
    
    #count_rows
    nrow()

nrow_num
## [1] 26

##Turn them into a function

count_number_of_colonys_by_state <- function(.data, state) {
     #body
    nrow_num <- .data %>%
    
    #filter rows by condition 
    filter(state == "Arizona") %>%
     #count columns 
     nrow()
    
    #return the new variable 
       return(nrow_num)
    }