A New Week!



We aren’t computer scientists and that’s okay!

We make lots of mistakes. Mistakes are funny. You can laugh with us.

Let’s go, Simba, Pumbaa, and Timon!





1 Some built-in functions for numbers


Almost everthing in R is done through functions.

Here are some commonly used functions that work with numbers.

1.1 Numeric functions


abs(x)       absolute value

sqrt(x)       square root – x^(1/2), x**(1/2)

round(x, digits = n)       round x to n decimal digits

log(x)       natural logarithm (base e)

log10(x)       common logarithm (base 10)

exp(x)       exponentials e^x

1.2 Statistical functions


mean(x, na.rm = FALSE)       mean of x

median(x)       median of x

sd(x)       standard deviation of x

quantile(x, probs)       quantiles of x with probability

range(x)       smallest to largest values

max(x)       maximum

min(x)       minimum

sum(x)       summation

2 Write functions


One of the great strengths of R is user can write customized functions.

In general, if you find yourself doing a lot of cutting and pasting, that’s usually a good sign to write a function.

Functions are defined using the function() and are stored as R objects just like anything else.

2.1 Argument matching

R functions arguments can be matched by position or by name.

Position matching just means that R assigns the first value to the first argument, the second value to second argument.

2.2 Argument with a default value

We must provide the argument values in the obtain_match() function if defualt values are not available

We can modify the function to avoid such error by providing default values for arguments

3 Recap



  • Some built-in functions for numbers

  • Write functions