Lecture 1: R Basics and Tips



Yanfei Kang
yanfeikang@buaa.edu.cn

School of Economics and Management
Beihang University

In this lecture we are going to learn …

Packages required in this lecture

install.packages(c("forecast", "sos", "formatR"))

R packages



How to install and use R

Getting and Installing R

R User Interface

Editor

Use an IDE: Rstudio



R basics

Basic Operations

# simple maths
1 + 2 + 3
1 + 2 * 3 

# assign a value to a variable
x <- 1
y <- 2
z <- c(x,y)
z

# function examples
exp(1)
cos(3.141593)
log2(1)

Vectors

# vectors
c(0, 1, 1, 2, 3, 5, 8)
1:10
seq(1, 9, 2)
rep(1, 10)
length(rep(1, 10))

# character vectors
c("Hello world", "Hello R interpreter")

# vector calculation
c(1, 2, 3, 4) + c(10, 20, 30, 40)
c(1, 2, 3, 4) + 1

# you can refer to elements by location in a vector
b <- c(1,2,3,4,5,6,7,8,9,10,11,12)
length(b)
b
b[7]
b[1:6]
b[c(1,6,11)]
b > 5
b[b > 5]

Matrix

# create a matrix
m <- matrix(c(1:6), 2, 3)
n <- matrix(c(8:13), 2, 3)
dim(m)
t(m)
m[1, 2]
m[1, ]
cbind(m, n)
rbind(m, n)

List

l <- list(a = c(1, 2), b = 'apple')

Data frame

L3 <- LETTERS[1:3]
fac <- sample(L3, 10, replace = TRUE)
d <- data.frame(x = 1, y = 1:10, fac = fac)

Functions

f <- function(x, y) {
  z <- c(x + 1, y + 1)
  return(z)
}
f(1, 2)

The if condition

x <- 0
if (x > 1) {
  print('x is larger than 1')
} else {
  print('x is not larger than 1')
}

Loops

x <- 1:10
for(i in x) {
  print(i^2)
}

Your turn (1)

  1. Write a function MySummary() where the input argument is x can be any vector and the output is a list that contains the basic summary (mean, variance, length, max and minimum values) of the vector you have supplied to the function.
  2. Test your function with some vectors (that you make up by yourself).

Answer (1)

MySummary <- function(x){
  x.mean <- mean(x)
  x.var <- var(x)
  x.length <- length(x)
  x.max <- max(x)
  x.min <- min(x)
  x.summary <- list(mean = x.mean,
                    var = x.var,
                    length = x.length,
                    max = x.max,
                    min = x.min)
  return(x.summary)
}

# some tests
MySummary(c(1:10))
MySummary(seq(1, 100, 5))

Another option of Answer (2)

MySummary <- function(x){
  # firstly create a list
  x.summary <- list()
  # assigning values
  x.summary$mean <- mean(x)
  x.summary$var <- var(x)
  x.summary$length <- length(x)
  x.summary$max <- max(x)
  x.summary$min <- min(x)
  return(x.summary)
}

# some tests
MySummary(c(1:10))
MySummary(seq(1, 100, 5))



R coding style

File names

Choose the names of variables and functions carefully

What we should do

What we should not do

White Spaces

x <- c(1:10)
x.average<-mean(x,na.rm=TRUE)

\(\Rightarrow\)

x.average <- mean(x, na.rm = TRUE)

Don’t be afraid of splitting one long line into individual pieces!

n <- matrix(sample(1:100, 9), nrow = 3, ncol = 3, byrow = TRUE)

\(\Rightarrow\)

n <- matrix(sample(1:100, 9),
           nrow = 3,
           ncol = 3,
           byrow = TRUE)

Curly braces

if (y < 0) {print("y is negative")}

\(\Rightarrow\)

if (y < 0) {
  print("y is negative")
}

Indenting

if (y < 0) {
print("y is negative")
}

\(\Rightarrow\)

if (y < 0) {
  print("y is negative")
}

Make your code tidy in a second!

Header, Line spaces and Comments

x <- c(1:10)
x.mean = mean(x)
x.var = var(x)

\(\Rightarrow\)

# =============================================
# Title
# Author: Yanfei Kang
# Date: Mar 23, 2017
# Description: your purpose
# =============================================

x <- c(1:10)

# getting the mean of x
x.mean = mean(x)

# getting the variance of x
x.var = var(x)

Function Documentation

CalculateSampleCovariance <- function(x, y, verbose = TRUE) {
  # Computes the sample covariance between two vectors.
  #
  # Args:
  #   x: One of two vectors whose sample covariance is to be calculated.
  #   y: The other vector. x and y must have the same length, greater than one,
  #      with no missing values.
  #   verbose: If TRUE, prints sample covariance; if not, not. Default is TRUE.
  #
  # Returns:
  #   The sample covariance between x and y.
  n <- length(x)
  # Error handling
  if (n <= 1 || n != length(y)) {
    stop("Arguments x and y have different lengths: ",
         length(x), " and ", length(y), ".")
  }
  if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) {
    stop(" Arguments x and y must not have missing values.")
  }
  covariance <- var(x, y)
  if (verbose)
    cat("Covariance = ", round(covariance, 4), ".\n", sep = "")
  return(covariance)
}



Some nice R tips

How to find the right function

library(forecast)
help.search("auto.arima")
??auto.arima
library(sos)
findFn("arima")
RSiteSearch("arima")

Digging into functions

Organize your R projects

Basic idea

Look at other people’s codes

Getting help

Keep up-to-date

Your turn (2)

FindRoots <- function(a = 1, b = 1, c = 0) {
  # Computes the roots of a quadratic equation.
  #
  # Args:
  #   a: The coeffienct of x^2. Default is 1.
  #   b: The coeffienct of x. Default is 1.
  #   c: The constant. Default is 0.
  #
  # Returns:
  #   Roots.
  if (b ^ 2 - 4 * a * c < 0) {
    return("No real roots")
  } else {
    root1 <- (-b + sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
    root2 <- (-b - sqrt(b ^ 2 - 4 * a * c)) / (2 * a)
    roots <- c(root1, root2)
    return(roots)
  }
}

# some tests
FindRoots()   # using default values of arguments
FindRoots(1, 2, 0)
FindRoots(1, 2, 3)

Your turn (3)

References

Further readings