7 Setup: Function Basics

Purpose: Functions are our primary tool in carying out data analysis with the tidyverse. It is unreasonable to expect yourself to memorize every function and all its details. To that end, we’ll learn some basic function literacy in R; how to inspect a function, look up its documentation, and find examples on a function’s use.

Reading: Programming Basics. Topics: functions, arguments Reading Time: ~ 10 minutes

7.0.1 q1 How do you find help on a function? Get help on the built-in rnorm function.

?rnorm

7.0.2 q2 How do you show the source code for a function?

rnorm
## function (n, mean = 0, sd = 1) 
## .Call(C_rnorm, n, mean, sd)
## <bytecode: 0x7fd6458d8b00>
## <environment: namespace:stats>

7.0.3 q3 Using either the documentation or the source, determine the arguments for rnorm.

The arguments for rnorm are:

  • n: The number of samples to draw
  • mean: The mean of the normal
  • sd: The standard deviation of the normal

7.0.4 q4 Scroll to the bottom of the help for the library() function. How do you

list all available packages?

Calling library() without arguments lists all the available packages.

The examples in the help documentation are often extremely helpful for learning how to use a function (or reminding yourself how its used)! Get used to checking the examples, as they’re a great resource.