What is R?

Getting started

In this class we are going to use RStudio Cloud for in-class exercises and assignment. I have sent you an email invitation to join the course home page in RStudio Cloud. You can run all everything we do in RStudio Cloud on your local computer.

RAM and HDD

RAM and HDD, are both types of computer memory. RAM is used to store computer programs and data that CPU needs in real time. It is a working memory of the computer. RAM data is volatile and is erased once computer is switched off. HDD, hard disk has permanent storage and it is used to store user specific data.

Types of objects

  1. character: “LSU”
  2. numeric: 5.5342
  3. integer: 7
  4. complex: 1+4i
  5. logical: TRUE, FALSE or T,F for short

Interactive Session

Assignment operator

x = 1
y = 2
x+y
## [1] 3
text = "hello"
print(text)
## [1] "hello"

Listing objects

ls(): Lists all the objects

ls()
## [1] "text" "x"    "y"

rm(list=ls()): clears all the objects

rm(list=ls())
ls()
## character(0)

Vector: a combination of same class

a  = c("a","b","c")
print(a)
## [1] "a" "b" "c"
b = 1:10 # : operator is used to create integer sequence
print(b)
##  [1]  1  2  3  4  5  6  7  8  9 10
print(b[4:8])
## [1] 4 5 6 7 8
d  = c(TRUE,FALSE)

Factors: Used to represent categorical data

  • Can be ordered (agree, disagree, …) or unordered (male, female)
  • More descriptive and easier to interpret
  • You can think of factors as integer vector with each having a label
fcts = factor(c("male","female","male","female"),levels=c("male","female"))
print(fcts)
## [1] male   female male   female
## Levels: male female

Missing Values

  • denoted by NA or NaN (not a number)
  • is.na(): checks if an element is not a number
  • is.nan()

R Scripts

First R Script

rm(list=ls()) # clears the memory

# creates a vector of 25 random numbers between 0 and 1
random_data = runif(25, 0, 1)

# calculate mean
random_data_mean = mean(random_data)

# calculate standard deviation
random_data_sd = sd(random_data)

# print results
print(paste("Mean: ",random_data_mean,", SD: ",random_data_sd))
## [1] "Mean:  0.42137958179228 , SD:  0.312131304101719"

R Markdown

Control Structures

Control structures allow programmer to control the structure of the program. We are going to talk about ‘if else’, ‘for’, ‘while’, etc.

if-else conditions

Suppose you want to assign a credit rating based on the probability of default. If the probability of default is less than 1% the credit rating would be ‘A’. If the probability of default is greater than 1% but less than 5% we want to assign a credit rating of ‘B’. 5% or greater probability of default would be assigned a rating of ‘C’.

creditrating = NA
probofdefault = 0.02

if(probofdefault<0.01) {
  creditrating <- 'A'
} else if(probofdefault<0.05) {
  creditrating <- 'B'
} else {
  creditrating <- 'C'
}

print(paste("Credit rating: ", creditrating))
## [1] "Credit rating:  B"

for loop

Print the squreroots of numbers from 1 to 10

for(i in 1:10) {
  print(sqrt(i))
}
## [1] 1
## [1] 1.414214
## [1] 1.732051
## [1] 2
## [1] 2.236068
## [1] 2.44949
## [1] 2.645751
## [1] 2.828427
## [1] 3
## [1] 3.162278

while loop

Count by 5 up to 100

no = 0
while(no<=100) {
  print(no)
  no = no+5
}
## [1] 0
## [1] 5
## [1] 10
## [1] 15
## [1] 20
## [1] 25
## [1] 30
## [1] 35
## [1] 40
## [1] 45
## [1] 50
## [1] 55
## [1] 60
## [1] 65
## [1] 70
## [1] 75
## [1] 80
## [1] 85
## [1] 90
## [1] 95
## [1] 100

break

Iterate through the given list of letters and stop if the letter is equal to ‘f’

letters <- c("s","r","e","x","f","a","o")

for(l in letters) {
  if(l=="f") {
    break
  }
  print(l)
}
## [1] "s"
## [1] "r"
## [1] "e"
## [1] "x"

functions

Write a function to add 2 numbers

add2 <- function(a,b) {
  return (a+b)
}

print(add2(4,5))
## [1] 9

Write a function to calculate the present value of a cashflow

presentvalue <- function(fv,n,r) {
  return(fv/(1+r)^n)
}

print(presentvalue(121,2,0.1))
## [1] 100

Reusing functions

  1. Open a new R script
  2. Write your function
  3. Save it as an R file
  4. Use ‘source’ command to import the function

Write a function to calculate the net present value of a project

netpresentvalue <- function(cashflows,r) { t = 1 npv = 0 for(c in cashflows) { npv <- npv+cashflows[t]/(1+r)^(t-1) t=t+1 } return(npv) }

source('functions.R')
print(netpresentvalue(cashflows=c(-100,110),r=0.1))
## [1] -1.421085e-14

Coding Standards

Make your coding readable and easy to understand