6 Writing functions

Writing own R functions

  • Every R function has three parts:
    • name
    • a body of code
    • a set of arguments
  • The basic format of an R function
  • Functions are just another R object

  • Comas separate more than one argument

Example

  • Write a function that calculates the area of a right triangle
  • It takes two arguments: base and height.
#> [1] 3

Simulating rolling dice

The following codes simulate an experiment “rolling a dice”

#> [1] 3
#> [1] 1

Write a function roll() for the simulation

  • roll() has no arguments, and it will return a number between 1 to 6

Simulating rolling dice

  • To use the function roll(), first run the written R function codes in the R console. Then run roll()
#> [1] 2
#> [1] 1
#> function () 
#> {
#>     dice <- 1:6
#>     out <- sample(x = dice, size = 1)
#>     return(out)
#> }
#> <bytecode: 0x7fe6852f24a8>

Functions with arguments

The number of heads in 10 fair coin toss

#> [1] 4

The number of heads in 20 fair coin toss

#> [1] 15

The number of heads in 100 fair coin toss

#> [1] 52

Functions with arguments

  • The size argument of sample() is changing for different number of coin toss

  • Can you write a function in R that takes how many times to toss a fair coin and prints us how many times it lands on heads?

Functions with arguments

Tossing coins 200/500 times

#> [1] 101
#> [1] 261

What will happen if you run the function without specifying the argument

Functions with arguments

  • Default value of an argument can be specified

  • E.g., times = 100 will be considered if times is not supplied!

#> [1] 55
#> [1] 5008

Exercise 6.1

  • Write an R function roll_dice() that rolls a dice n times (where n is 1 or more) and returns the sum of the dice rolls.

  • Write an R function toss_bias_coins() that tosses a biased coin n times (where n is 1 or more times) and returns the number of heads. The probability of getting heads is 0.70.

  • Write an R function that can simulate flipping a fair or biased coin. The probability of getting heads should not always be 0.70, but any value between 0 and 1!

Help for R functions

help("mean") or “?mean” to check the detail of mean()

  • The help.search() function searches through the help documentation, code demonstrations and package vignettes and displays the results as clickable links for further exploration

  • Another useful function is apropos()

#>  [1] ".colMeans"     ".rowMeans"     "colMeans"      "kmeans"       
#>  [5] "mean"          "mean.Date"     "mean.default"  "mean.difftime"
#>  [9] "mean.POSIXct"  "mean.POSIXlt"  "rowMeans"      "weighted.mean"

Help for R functions

Pro Tip

RStudio snippet can save your time. Type fun and wait some milliseconds to see suggestions (press Tab if they don’t appear). Select (or press Tab) on the snippet option to insert it. You can see and customize your snippets from Tools>Global Options>Code.

1 / 12
6 Writing functions

  1. Slides

  2. Tools

  3. Close
  • 6 Writing functions
  • Writing own R functions
  • Example
  • Simulating rolling dice
  • To use the function...
  • Functions with arguments
  • The size argument...
  • Tossing coins 200/500...
  • Default value of...
  • Exercise 6.1
  • Help for R functions
  • Pro Tip RStudio...
  • f Fullscreen
  • s Speaker View
  • o Slide Overview
  • e PDF Export Mode
  • r Scroll View Mode
  • ? Keyboard Help