# Introduction to the R Computing Language

### Written by: Steve McKelvey Saint Olaf College Department of Mathematics, Statistics and Computer Science

This is an evolving document designed to support the use of R in Modern Computational Mathematics (Math 242). As the course progresses through the term, useful features of the R language will be added to this document to provide a reference resource for students in the class.

Most of the entries here will be modifications of materials that appear on the official R project website. Of particular use to me has been the introduction to R page found under the Manuals section of the project website.

• Getting R at St. Olaf:  St. Olaf maintains a standard installation for R that runs on the college's Windows machines. On college machines that run R, you should see an R icon on the desktop.
• Getting R On Your Own Computer:  R is free software which can be run on Windows, Mac OS and Unix-like operating systems. Instructions for obtaining and installing R on these systems can be found at this website.
• Assignment
Function/OperatorExplanation
<- To give a name to a vector, use a leftward pointing arrow, creating by using two typed characters, the less-than symbol and the minus symbol. The name to be given to the vector should appear on the left, with the vector itself appearing on the right.
Example: vec <- 1:10
= Same as above. Example: vec = 1:10
• Building Vectors
Function/OperatorExplanation
c()The concatenation function c() takes any number of vectors as an argument and returns the concatenation of those vectors as a single, longer vector.
Example: c(1,2,c(3,4))⇒(1,2,3,4)
Regular
Sequences
Two integers, the first less than the second, separated by a colon generates a sequence containing all the integers from the first through the second, inclusive, in increasing order.
Example: -2:5 ⇒(-2,-1,0,1,2,3,4,5)
seq() The seq() function allows us to create regular sequences with intervals other than 1. The first two arguments to seq() are the starting and stopping numbers, not necessarily integers. The third argument is an expression of the form "by=nnnn" where nnnn is replaced by the step size you want.
Example: seq(-2,1,by=0.4) ⇒ (-2,-1.6,-1.2,-0.8,-0.4,0,0.4.,0.8)
Other versions of seq() are also available.
rep() The rep() function allows us to build vectors by repeating the entries in an already existing vector. The first argument to rep() is an existing vector, the second argument is an expression of the form "times=nnnn" where nnnn is the number of times the vector will be replicated.
Example: rep(c(1,2,3),times=4)⇒(1,2,3,1,2,3,1,2,3,1,2,3)
• Binary Operators

R uses many operators. In most cases, binary operators take two vectors as arguments and return a single vector with length equal to the longer of the two arguments. The operator is applied componentwise to the two arguments, with the shorter argument vector wrapping around to provide enough entries to pair with the entries of the longer vector.
Function/OperatorExplanation
Componentwise Addition: + Example: c(2,3) + c(4,5,6) ⇒ (2,8,8)
Componentwise Subtraction: - Example: c(2,3) - c(4,5,6) ⇒ (-2,-2,-4)
Componentwise Multiplication: * Example: c(2,3) * c(4,5,6) ⇒ (8,15,12)
Componentwise Division: / Example: c(2,3) / c(4,5,6) ⇒ (0.5, 0.6, 0.33333)
Componentwise Remainder: %% Example: c(12,13) %% c(4,5,6) ⇒ (0, 3, 0)
Componentwise Integer Division: %/% Example: c(12,13) %/% c(4,5,6) ⇒ (3,2,2)

• Numerical Computations on Vectors
Function/OperatorExplanation
sum() The sum() function computes the sum of all the entries in a vector, returning the result as a scalar (a vector containing one number).
Example: sum(c(1,3,5)) ⇒ 9
cumsum() The cumsum() function takes one argument, a vector, and returns a vector of the same size. The n-th entry in the new vector is the sum of the first n entries in the argument.
Example: cumsum(c(1,3,5,7,9)) ⇒ (1,4,9,16,25)
cumprod() The cumprod() function takes one argument, a vector, and returns a vector of the same size. The n-th entry in the new vector is the product of the first n entries in the argument.
Example: cumprod(c(1,3,5,7,9)) ⇒ (1,3,15,105,945)
• Plotting Functions

There are many R functions associated with the creation of plots. A key idea in understanding R plots is the creating complicated plots is a two fold process. First, an initial plot is created, typically with the plot() function. This determines the size of the plot as well as the x- and y- ranges. This plot becomes the "active" plot to which additional lines and symbols can be added using the lines() function and others.
Function/OperatorExplanation
univariate plot() One argument, a vector. The values in the vector will be considered y-values and the index of the value (1,2,3,...) will be the x-value of the points plotted.
bivariable plot() A scatter plot is produced where the (x,y) coordinates of the points plotted come from the corresponding positions of the first and second arguments, x-values coming from the first vector, y-values from the second.
Other plot() arguments type="l" (letter l, not number 1) connects the dots
xlim=c(x1,x2) directs plot to use [x1,x2] as x-axis plot limit.
ylim=c(y1,y2) directs plot to use [y1,y2] as y-axis plot limit.
lines() Same syntax as plot() but adds lines to existing plot rather than creating a new one.

• Matrix Operations
Function/OperatorExplanation
matrix(data,nrows,ncols) This creates a matrix with the indicated number of rows and columns. The first argument is a vector containing the entries to be loaded into the matrix. These entries fill by column, meaning the first column is filled before the second column is started, etc.
%*% Matrix multiplication is performed by the binary operator %*%. Any vectors appearing in the expression are treated as either row or column vectors, as needed to make sense of the expression.
solve(A)If A is a square matrix, solve(A) computes the matrix inverse of A if the inverse exists.
solve(A,b) If A is a square, invertible matrix and b is a vector of suitable length, solve(A,b) provides the solution of the linear system Ax=b.
t(mat) This returns the transpose of the matrix `mat`.
• Creating New Functions

The following snippet of R code creates a new function that adds together three numbers (or vectors).

``` add3nums <- function(n1,n2,n3) { n1 + n2 + n3 } ```

To use this function you would enter something like:

`add3nums(1,2,3)`

Disclaimer