Making Functions practice

Schwab

Functions

  • Inputs and outputs

  • Can set defaults arguments

  • Can be general or specific to a package.

    • n() vs. read_all_contributions()
  • Save time in the long run.

Function outline

Greeting <- function(arg1 = "Hello" ){
  print(arg1)
}
Greeting()
[1] "Hello"
Greeting("Hiya")
[1] "Hiya"

This function will add

adding <- function(x,y){
  
  total <- sum( c( x , y ))
  
  return(total)
}

adding(2,4)
[1] 6
#adding()

This function will multiply

multiplying <- function(x = 1, y = 1){
  total <- x*y
  total
}

multiplying()
[1] 1
multiplying(2,4)
[1] 8

Control Flow

If, else if , else

Make a magic 8 ball.

#Defining the function
magic_8_ball <- function(){

#Making a set of values to choose from
sample_set <- c(-1,0,1)  
  
#Randomly choosing a value
random_number <- sample(sample_set, size = 1)  
  
# Making a couple of vectors with 8 ball sayings
positive_vibes <- c("It is certain",
                    "It is decidedly so",
                    "Most likely",
                    "Signs point to yes")

neutral_vibes <- c("Reply hazy, try again",
                   "Ask again later",
                   "Concentrate and ask again")

negative_vibes <- c( "Don't count on it", 
                     "My reply is no", 
                     "Very doubtful")
# Control flow
if(random_number == 1){
    print(
      sample(positive_vibes, size = 1)
    )
    }
    
else if(random_number == 0){
    print(
      sample(neutral_vibes, size = 1)
    )
}
  
else{
    print(
      sample(negative_vibes, size = 1)
    )
    
    }
}

# Run the function
magic_8_ball()
[1] "Reply hazy, try again"

A composite function

Depending in an argument

my_1st_calculator <-function(x,y,operation="add"){
  
  if(operation %in% "add"){
    total <- adding(x,y)
  }
  
  else if(operation %in% "multiply"){
    total <- multiplying(x,y)
  }
  
  else{
    stop("Please choose `add` or `multiply` for the operation argument")
  }
  
  return(total)
}
my_1st_calculator(1,6, operation="multiply")
[1] 6

R is vectorized

This means that R will do operations on vectors.

vec1 <- c(1,2,3)
vec2 <- c(9,9,9)
vec1 + vec2
[1] 10 11 12

Try one

Make a function that takes a vector of students in this class and prints the name of each student.

library(tidyverse)
library(googlesheets4)

class_info <- read_sheet("https://docs.google.com/spreadsheets/d/1k9qA7XldtHB8-FZjmEymoUoeZJArS4tGy5gyVsvyLB0/edit?usp=sharing") |>
  janitor::clean_names()

# selecting only the column we are interested in and making it into a vector.
name_vector <- as.vector( 
  class_info |>
    select("preferred_name"))

Try these ones

  1. Make a function that takes a column of data and finds the average. Use it on the height variable by doing class_info$height (hint: try it with sum() and length(), or for an easier approach use mean()) .

  2. Make a function that draws the US state that is input ( hint: use the maps library).

  3. Challenge: Make it so that the user can override the magic 8 ball function to always get a positive answer. (Hint: I did this by adding an override argument. )