R Calculations and Factorials#

We often use R as a scientific calculator. When working in R, we often use the following functions.

Scientific Computing in R#

Basic Arithmetic#

We add, subtract, multiply, divide and indicate powers in the typical way, exactly as on a TI-84 graphing calculator. Suppose we wish to evaluate:

\[\left( 3 - 2^3 + \frac{12}{20} \right) 2\]
( 3 - 2^3 + 12 / 20 ) * 2
-8.8

Note that all multiplications must be indicated by an * . R does not understand that a quantity outside the parenthesis indicates multiplication by the quanity inside.

Summations#

If we have vector \(x\), we can sum all of its entries using sum(x). We also have a cool way to sum consecutive integers as in the handshake problem where we needed to evaluate

\[\sum_{k=1}^9k=45\]
sum(1:9)
45

The colon between two numbers creates a vector of consecutive integers from the first number to the second, and we can quickly confirm the famous Gaussian problem:

sum(1:100)
5050

Trigonometry#

The evaluations are simple once we understand the following:

  • R expects the input angles to be measured in radians.

  • R understands the keyword pi.

  • R does not understand the notation \(\sin^2\left(\frac{\pi}{4}\right)\).

sin(pi/4)
0.707106781186547
sin(pi/4)^2
0.5

We have confirmed that the trig functions work normally in R, and that

\[\begin{split}\begin{align}\sin\left(\frac{\pi}{4}\right) =& \frac{\sqrt{2}}{2} \implies \\ \sin^2\left(\frac{\pi}{4}\right) =& \left(\frac{\sqrt{2}}{2}\right)^2 = \frac{1}{2}\end{align}\end{split}\]

Factorials, Combinations and Permutations#

Counting problems in probability rely heavily upon combinations and permutations which, in turn, are built upon the factorial.

The Factorial() Function#

factorial(5)
120

Permutations and Combinations#

Native R includes neither a permutations formula nor a formula for combinations. However, we can easily write formulas for both.

Combinations Formula#

Mathematically, when we say “n choose r,” we mean:

\[\binom{n}{r} = \frac{n!}{r!(n-r)!}\]

Example: \(\binom{7}{5}\)#

We have the following calculation to evaluate:

\[\begin{split}\begin{align}\binom{7}{5} &= \frac{7!}{5!2!}\\&= \frac{7*6*5*4*3*2*1}{(5*4*3*2*1)(2*1)}\\&=\frac{7*6}{2}\\&= \frac{42}{2}\\&= 21\end{align}\end{split}\]

Writing a Combinations Function in R#

We have a factorial function in R, so creating a function that evaluates the top line of the equations above is reasonably straightforward:

combin <- function(n, k) {
    return(factorial(n) / ( factorial(k)*factorial(n-k) )) }

Testing the function with \(\binom{7}{5}=21\), we see:

combin(7,5)
21

Permutations#

Notice the following relationship between the two formulas:

\[\begin{split}\begin{align}\text{Combinations: }\binom{n}{r}&=\frac{n!}{r!(n-r)!}\\\text{Permutations: }P(n,r)&=\frac{n!}{(n-r)!}\end{align}\end{split}\]

Hence, we see that

\[P(n,4) = \frac{n!}{(n-r)!} = r!\binom{n}{r}\]

Writing a Permutations Function in R#

Using the function for combinations, the function for permutations is quite easy to write:

perm <- function(n, k) {
    return(combin(n,k) * factorial(k))}

Example: 7 Permute 5#

Algebraically, we calculate:

\[P(7,5) = \frac{7!}{(7-5)!} = \frac{7*6*5*4*3*2*1}{2*1} = 7*6*5*4*3 = 2520\]
perm(7,5)
2520

Wrapping Up#

Please note that it will be useful to store these two functions where we can quickly copy-paste them into an R notebook for use doing classwork or homework.

combin <- function(n, k) {
    return(factorial(n) / ( factorial(k)*factorial(n-k) )) }
perm <- function(n, k) {
    return(combin(n,k) * factorial(k))}