1 A quick introduction to R

The following few commands demonstrate the basics of using R.

To create vectors, matrices and data frames

## creates two vectors x, y
n <- 30
x <- 1:n                           # a vector of (1,2,...,n)
x                                  # print x on screen
x[1:5]                             # print first 5 elements
x1 <- seq(from=0, to=1, by=0.1)    # a sequence with equal distance 0.1
x2 <- runif(n, min=0, max=1)       # realisations of U(0,1) rvs
x3 <- rbinom(n, size=1, prob=0.5)  # realisations of B(1,0.5) rvs
y <- rnorm(n, mean=0, sd=1)        # realisations of N(0,1) rvs
xx1 <- cbind(x, y)                # create a matrix by merging by column
dim(xx1)                           # dimension of matrix
xx2 <- rbind(x2, y)                # create a matrix by merging by row
dim(xx2)
DF <- data.frame(seq=x, unif=x2, norm=y, binary=x3)
DF                                 # data frame containing 4 variables
names(DF)                          # names of variables in data frame
DF$seq                             # access data frame contents
DF$norm

Summary statistics

mean(y)                  # sample mean
var(y)                   # sample variance
sd(y)                    # sample standard deviation
range(y)                 # min and max
median(y)                # median value
quantile(y, prob=0.25)   # 25%-ile
summary(y)               # a few summary statistics of vector
summary(DF)              # summary stats of each variable in data frame
table(x3)                # table of x3
table(rpois(n, lambda=1), x3)  # cross-table of Poi(1) samples and x3

Draw a few simple plots

## plots
plot(x, y)                  # scatter plot of x and y
plot(xx1)

par(mfrow = c(2, 1))        # arrange multiple plots on one page
plot(x, y)
lines(x, y, col="red")      # add lines
plot(x2, y)
lines(x2, y, col="blue")

hist(x2, prob=TRUE)         # histogram of x2 with probs not freqs
hist(y,col="yellow",20)     # histogram of y with 20 breaks

boxplot(x2)                    # boxplot of x2
boxplot(norm~binary, data=DF)  # boxplots of norm, split by bin

x11()ΨΨΨΨΨΨΨΨΨΨΨ  # open new plotting window
barplot(table(x3))          # Barplot of x3

R as a calculator

## mathematical functions
x <- 1:10
x*x               # elementwise multiplication
2^x               # elementwise power
x^2               # elementwise square
exp(x)            # elementwise exponential
log(x)            # nb: log(0) = -Inf
x + sin(x*pi/4)   # nb: pi = 3.41593

## square matrix and inverse
A <- matrix(c(2,1,-1,3), nrow=2, ncol=2) # read column-wise
A
A[1,]ΨΨΨΨΨΨΨ# row 1
A[,2]ΨΨΨΨΨΨΨ# column 2
A[2,1]ΨΨΨΨΨΨ# element at row 2 and column 1
t(A)              # transpose of A
solve(A)          # inverse of A
B <- matrix(c(1,2,1,2), nrow=2, ncol=2)
solve(B)          # non-invertible, matrix is singular
det(B)ΨΨΨΨΨΨ# determinant of B

## matrix multiplication
A%*%B

Using for loops
Repeating operations can be made simple by using for loops.

out <- matrix(0, nrow=20, ncol=2)  # storage
for (i in 1:20){  # define loop variable and set of values to take
  a=2*i
  b=3+i
  out[i,1] <- a
  out[i,2] <- b
  # out[i,] <- c(a,b) # store as a row vector each time
}
out

How to find help
To access the help page,

help.start()  # opens webpage, click on "An Introduction to R"
?runif        # opens manual page for simulating uniform rvs
help(lm)      # opens manual page for linear regression

Installing and loading libraries
By default, R contains a limited number of functions for common need. There are 100s of packages available on the Comprehensive R Archive Network (CRAN) that contain functions for specialist use.

install.packages("censReg")  # install package
                             # select a local CRAN mirror (UK, Bristol)
library("censReg")           # load in library

Accessing data in R
There are some example data sets available in the base package of R.

data()                 # lists data sets available
data(women)            # makes women available
?women                 # information about the data
names(women)           # name of the variables
summary(women)         # data frame summary
attach(women)          # adds women to the search list
summary(height)        # The same variable now available by name
detach(women)          # remove women to the search list
summary(height)        # comprehend?

Loading data from file.
There are a few ways to load data into R depending on the file format.

load("DATA.Rdata")      # file type .Rdata
read.table("DATA.txt")  # file type .txt
read.table("DATA.dat")  # file type .dat
read.csv("DATA.csv")    # file type .csv

Remember to change your working directory using setwd("PATH") or put the full path to the data file in the above commands.

Memory management

ls()                 # list items in memory
a <- "NEW OBJECT"    # create new character object
ls()                 # object "a" appears in list
rm(a)ΨΨΨΨΨΨΨΨ # remove "a" from memory
ls()