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 everything we do in RStudio Cloud on your local computer.
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.
x <- 1
y <- 2
x+y
## [1] 3
text <- "hello"
print(text)
## [1] "hello"
ls(): Lists all the objects
ls()
## [1] "text" "x" "y"
rm(list=ls()): clears all the objects
rm(list=ls())
ls()
## character(0)
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)
fcts <- factor(c("male","female","male","female"),levels=c("male","female"))
print(fcts)
## [1] male female male female
## Levels: male female
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.50355591090396 , SD: 0.312337265044136"
Control structures allow programmer to control the structure of the program. We are going to talk about ‘if else’, ‘for’, ‘while’, etc.
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.002
if(probofdefault<0.01) {
creditrating = "A"
} else if(probofdefault<0.05) {
creditrating= "B"
} else {
creditrating = "C"
}
print(paste("Credit Rating:",creditrating))
## [1] "Credit Rating: A"
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
numbervec <- c(5,7,9)
for(i in numbervec) {
print(i)
}
## [1] 5
## [1] 7
## [1] 9
Count by 5 up to 100
Print numbers from 1 to 10, except numbers 3 and 7.
for(j in 1:10) {
if(j %in% c(3,7)) {
next
}
print(j)
}
## [1] 1
## [1] 2
## [1] 4
## [1] 5
## [1] 6
## [1] 8
## [1] 9
## [1] 10
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(c in letters) {
if(c=="f") {
break
}
print(c)
}
## [1] "s"
## [1] "r"
## [1] "e"
## [1] "x"
A function is a block of code that is used to perform a task. A function typically takes and input, executes the block of code, and returns a value.
More: https://www.youtube.com/watch?v=Pi0Yf-jn7O8
add2 <- function(a,b) {
output = a+b
return(output)
}
add2(2,6)
## [1] 8
presentvalue <- function(fv,r,n) {
pv = fv/(1+r)^n
return(pv)
}
presentvalue(245,0.05,3)
## [1] 211.6402
cf = c(-10,5,4,5,1)
r = 0.14
npv = 0
for(index in 1:length(cf)) {
pv = cf[index]/(1+r)^(index-1)
npv = npv+pv
}
print(npv)
## [1] 1.430773
Write a function to calculate the net present value of a project
npv <- 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('npv.R')
cf = c(-100,120,34,56,23,54)
r = 0.08
npv(cf,r)
## [1] 138.3724
Make your coding readable and easy to understand