March 4, 2020

Functions in R

  • How do you write a new function in R?
  • What are its parts? How many of each one?

Functions in R

How do you write a new function in R?

 name <- function(input) {
   commands; 
   return(output)
 }

 

 

What are its parts? How many of each one?

  • name, only one.
  • input, zero or more.
  • commands, one or more.
  • output, only one or zero.

Example: drawing a stick-person

draw_person <- function(size) {
  draw_head(size)
  turtle_left(180)
  turtle_forward(size/2)
  turtle_left(90)
  draw_arm(size)
  turtle_left(180)
  draw_arm(size)
  turtle_left(90)
  turtle_forward(size*3/2)
  turtle_left(40)
  draw_leg(size)
  turtle_right(40)
  draw_leg(size)
}

 

 

  • What is the name?
  • What are the inputs?
  • What is the output?
  • What is the code?

Sub-tasks

Write the functions draw_head(), draw_arm() and draw_leg().

Each function must leave the turtle in the same position and the same angle as before

Your function can move the turtle as you wish, but it must leave the turtle as it was at the beginning of the function

This is a Contract

Each part commits to do something

Each part makes a promise

We promise to leave the turtle in the same position as we received it

Arm and Leg

draw_arm <- function(size) {
    turtle_forward(size)
    turtle_backward(size)
}
draw_leg <- function(size) {
    old_pos <- turtle_getpos()
    old_angle <- turtle_getangle()
    turtle_forward(size*3/2)
    turtle_left(90)
    turtle_forward(size/2)
    turtle_setangle(old_angle)
    turtle_setpos(old_pos[1], old_pos[2])
}

Head

Making Decisions

Smallest of two values

The contract is this:

  • Our function will get two numbers, let’s say a and b
  • We must return one number: the smallest value between a and b

Let’s write a function for this

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What is the code?

Answer now

How do we decide which one is the smallest one?

Making a decision in Scratch

if <> then

if then else

There are two version: if-then and if-then-else

These command takes one logic condition and one or two code blocks

Most of times we use only if-then

Conditionals in R

if(cond) {
    # do this only if `cond` is true
}
if(cond) {
    # do this only if `cond` is true
} else {
    # do this only if `cond` is false
}

For the “Smallest of two values”

  • if a is smaller than b
    • then the answer is a
  • else (a isn’t smaller than b)
    • then the answer is b
if(a<b) {
    answer <- a
} else {
    answer <- b
}

Smallest of three

The contract is this:

  • Our function will get three numbers, let’s say a, b, and c
  • We must return one number: the smallest value between a, b, and c

Let’s write a function for this

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What is the code?

Answer now

Smallest of many

The contract is this:

  • Our function will get a vector of numbers, let’s say x
  • We must return one number: the smallest value of x

Let’s write a function for this

Making a function

  • What will be the name?
  • What will be the inputs?
  • What will be the output?
  • What is the code?

Answer now

Exercise

Make these functions using only for(), if() and indices. You cannot use the words min or max. The input is a vector called x.

Please write the code of the following functions:

  1. smallest_value(x). Returns the smallest element in x.
  2. largest_value(x). Returns the largest element in x.
  3. place_of_smallest(x). Returns the index of the smallest element in x.
  4. place_of_largest(x). Returns the index of the largest element in x.

You can test your function with the following code.

x <- sample(5:20, size=10, replace=TRUE)
min(x)
smallest_value(x)

The two results must be the same.

(We will study the function sample later)