Basic Functions

We introduce basic functions in the base R package that will will be used frequently.

Matrices

A function can be called or invoked, if already defined, whether manually or otherwise, by appending a pair of parentheses after the name of the function. The parentheses, as in functions that we learn about in high school mathematics, often, but need not, include a comma-separated list of inputs. A function defined by R’s base package is matrix(). We can find out more about the function by using the following command: ‘?matrix’.

The help file indicates that the matrix() function takes several inputs, but we focus on the first three: (1) the data; (2) the number of rows; and (3) the number of columns.

We create a simple matrix.

x<-matrix(data=c(1,2,3,4),nrow=2,ncol=2)
x
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

A look at x tells us that the data c(1,2,3,4) populates the matrix by successfully filling in the colunns. We can change this behavior by adding an additional argument to the matrix() function.

matrix(c(1,2,3,4),2,2,byrow=TRUE)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

Generate Normal Random Variables & Use the cor() Function to Find Correlation

The rnorm() function generates a vetor of random normal variables from the normal distribution. The first argument to the rnorm() function is the number of number of variables we wish to generate. Below, we generate two correlated sets of numbers, x and y, and use the cor() function to compute the correlation between the x and y.

x<-rnorm(50)
y<-x+rnorm(50,mean=50,sd=.1)
cor(x,y)
## [1] 0.9928069

By default, the rnorm() function generates samples from a Gaussian distribution with mean 0 and standard deviation 1. To compare results, i.e., to generate identical “random” results, we use the set.seed() function as a preliminary step.

Graphics

Use the plot() function to produce simple scatterplot. For instance:

x<-rnorm(100)
y<-rnorm(100)
plot(x,y,xlab="x-axis",ylab="y-axis",main="plot of x vs. y")

We can save the output of an R plot to an external file in pdf or jpeg formats (among others).

pdf("figure.pdf")
plot(x,y,col="green")
dev.off()
## png 
##   2

Create Sequences of Numbers

The examples below show how to use the seq() function.

x<-seq(1,10)
x
##  [1]  1  2  3  4  5  6  7  8  9 10
# Shorthand for using seq() function with two arguments
z<-1:10
z
##  [1]  1  2  3  4  5  6  7  8  9 10
# z and x are identical
x<-seq(-pi,pi,length=50)

More Sophisticated Plots

The contour() function produces a contour plot to represent 3D data, and takes three arguments:
1. a vector of x values
2. a vector of y values
3. a matrix whose elements correspond to the z value for each pair of (x,y) coordinates.

x<-seq(-pi,pi,length=50)
y<-x
f<-outer(x,y,function(x,y)cos(y)/(1+x^2))
contour(x,y,f)
contour(x,y,f,nlevels=45,add=T)

fa<-(f-t(f))/2
contour(x,y,fa,nlevels=15)

The image() function is similar to contour() but adds a color-coded plot with colors that deped on the z-value. This produces what is known as a heatmap.

image(x,y,fa)

The persp() functino is used to produce the plot in three dimensions, where the inputs theta and phi control the angles at which the plot is viewed.

persp(x,y,fa)

persp(x,y,fa,theta=30,phi=20)