RStudio provides an excellent interface that makes working with R whole lot easier. If you haven’t done so, please first install the latest version of R before you install RStudio.
R. You should be able to see something like:At this stage, you can actually start using basic functions of R, for example,
But there is a much easier alternative to run and manage R programs. RStudio provides an excellent IDE. Now, go ahead and
Now, you’re ready to launch RStudio. You will see 4 windows.
To open a new R script file, Click File > New File > R script. This will be your first R script. R script is basically a text file in which you organize your commands and codes.
The bottom left window is the command “Console” in which you can execute your codes. For example, you can use R as a calculator, define and draw functions.
On the console just type
2 + 2## [1] 4
pi## [1] 3.141593
Or
log(exp(-5))## [1] -5
e <- exp(1)
print(e, digits = 3)## [1] 2.72
e <- exp(1)
print(e, digits = 17)## [1] 2.7182818284590451
Well, no surprises there. Now let’s try computing the number \(a = (9\times 8.1) + 8.1\) which is simply \(10\times 8.1 = 81\). Because multiplication has precedence over addition unless there is a parenthesis, we have
a <- 9*8.1 + 8.1
print(a)## [1] 81
But look at the digits after zero:
print(a, digits = 17)## [1] 80.999999999999986
This means the number 81 cannot be represented exactly in a computer which uses binary representation. The error in representing 81 is
print(81-a, digits = 17)## [1] 1.4210854715202004e-14
which is just \(1.4210854715202004\times 10^{-14}\), or
format(81-a, scientific = FALSE)## [1] "0.00000000000001421085"
Is this a problem? Well, it depends on your purpose and context. Suppose that you wanted to compare the output of the arithmetic operation to a known result. We can use logical operators for that (more on this below). Consider
81 == a## [1] FALSE
This is just another way of saying that two numbers are not exactly equal. But they are nearly equal:
all.equal(81,a)## [1] TRUE
with the default tolerance level of \(0.000000015\). In other words, the difference between 81 and the number a is less than the tolerance level. In passing, I note that the machine precision (sometimes called epsilon, \(\epsilon\)) in R is
print(.Machine$double.eps, digits=22)## [1] 2.2204460492503131e-16
Exercise: Find the largest number that can be represented in the computer smaller than 1.
Other arithmetic operations can be problematic as well. Try subtracting 10.1 from 10.2:
b <- 10.2 - 10.1
print(b, digits = 17)## [1] 0.099999999999999645
which is not exactly \(0.10\).
Exercise Show that the binary representation of 81 is \((1010001)\), i.e., \((81)_{10} = (1010001)_2\). Also find the binary representation of \(0.10\).
A package is a collection of functions. An R function is simply a series of instructions to perform a task or computation.
The base R comes with the following packages:
rownames(installed.packages(priority="base"))## [1] "base" "compiler" "datasets" "graphics" "grDevices" "grid"
## [7] "methods" "parallel" "splines" "stats" "stats4" "tcltk"
## [13] "tools" "utils"
R’s functionality can easily be extended by including software packages written by other R users. For example, we can install the data package for our textbook by typing the following code at the very top of the script (mind the quotation mark):
install.packages("ISLR", repos='http://cran.us.r-project.org')Alternatively, this can be done using Tools>InstallPackages from the menu and then typing “ISLR” on the search window. We basically downloaded this package into our computer. Now we can activate whenever we need it. To tell R that we will use we need a further command:
library(ISLR)This instructs R that our code needs to access contents of that package. Note that there is no quotation mark.
The data set package for the second edition of ISLR can be installed using
install.packages("ISLR2", repos='http://cran.us.r-project.org')Use getwd() to see the current directory. If we want to change it we can use setwd(path) where path should be defined using forward slash /, instead of backslash, \.
getwd()If we want to change it we can use setwd("/full/address/") where path should be defined using forward slash /, instead of backslash, \.
setwd("C:/Users/Folder/Another-Folder/MLinEcon")RR is case-sensitive so x and X are different objects. The content of an object is assigned using <- for example:
x <- 5
y <- 2
z <- x^y
z ## [1] 25
Note that the equation sign, =, can also be used but I suggest using <- (don’t forget to put a single space before and after).
List of all currently available objects in the memory can be accessed using the command ls()
ls()## [1] "a" "b" "e" "x" "y" "z"
Also see the Environment window in RStudio.
The command rm(x) removes the object x from the memory. If you want to remove all objects, you can use rm(list=ls()).
rm(list=ls())
ls()## character(0)
An R vector (or an atomic vector -not an algebraic vector-) is just a collection of objects. A vector my contain numeric elements (integers and doubles), logical elements, and characters. A vector can be defined in a number of ways. For example, we can use the function c() to create vectors a and b
a <- c(1,2,3,4,5)
a## [1] 1 2 3 4 5
b <- c(1,1,1,1,1)
a+b## [1] 2 3 4 5 6
integer <- c(10L, 20L)
typeof(a)## [1] "double"
typeof(integer)## [1] "integer"
char1 <- c("small", "medium", "large")
char1## [1] "small" "medium" "large"
typeof(char1)## [1] "character"
Let score be a vector with 10 elements containing evaluations on a new product out of 100 points.
score <- c(80, 65, 78, 94, 100, NA, 55, 85, 88, 75)
print(score)## [1] 80 65 78 94 100 NA 55 85 88 75
NROW(score)## [1] 10
length(score)## [1] 10
Some of the widely used R functions for vectors are illustrated below:
# number of elements in a vector
length(a)## [1] 5
# sort elements
sort(score)## [1] 55 65 75 78 80 85 88 94 100
# maximum
max(a)## [1] 5
# minimum
min(a)## [1] 1
# sum of the elements
sum(a)## [1] 15
# product of the elements
prod(a)## [1] 120
# vector of n zeros
numeric(5)## [1] 0 0 0 0 0
# create an n-vector with all elements a: rep(a, n)
rep(1,10)## [1] 1 1 1 1 1 1 1 1 1 1
# seq(T): create an integer sequence from 1 to T
seq(10)## [1] 1 2 3 4 5 6 7 8 9 10
# seq(t,T) similarly create a sequence from t to T
seq(5,10)## [1] 5 6 7 8 9 10
# seq(t,T,s) create a sequence from t to T by increment s
seq(1,10,2)## [1] 1 3 5 7 9
The elements of a vector can easily be accessed.
# first element
score[1]## [1] 80
# first three elements
score[1:3]## [1] 80 65 78
# last element
n <- length(score)
score[n]## [1] 75
# last three elements
score[(n-3):n]## [1] 55 85 88 75
Exercise Instead of running score[(n-3):n] use score[n-3:n]
score[n-3:n]## [1] 55 NA 100 94 78 65 80
Explain the output above.
Exercise Find the index of NA in the vector score and eliminate it. Call the new vector score2.
Solution The easiest way is to use two built-in functions which() and is.na() together. is.na() returns TRUE (more on that below) for NA entries, and FALSE otherwise.
# is an element NA ?
is.na(score)## [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
which() returns the index number, i.e., the position of the NA element in the vector.
# what's the position of NA
which(is.na(score))## [1] 6
Or, find the positions of entries that are not NA:
# indices of numeric elements (excluding NAs)
which(!is.na(score))## [1] 1 2 3 4 5 7 8 9 10
where we simply used the negation !is.na(). Now, exclude NA and create a new vector:
score2 <- score[which(!is.na(score))]
score2## [1] 80 65 78 94 100 55 85 88 75
Actually, using which() is redundant in this case (unless we want to retain intermediate steps):
score2 <- score[!is.na(score)]
score2## [1] 80 65 78 94 100 55 85 88 75
The function na.omit() can also be used to drop missing observations (to be discussed later).
A vector with logical elements:
logic1 <- c(TRUE, FALSE, TRUE)
print(logic1) ## [1] TRUE FALSE TRUE
Note that TRUE and FALSE are reserved in R. Here is the numerical representation of a logical vector:
as.numeric(logic1) ## [1] 1 0 1
We can use logical operators, ==, >, <, >=, <= ,!=, to create vectors with logical entries. For example,
score_less_than70 <- (score < 70)
score_less_than70## [1] FALSE TRUE FALSE FALSE FALSE NA TRUE FALSE FALSE FALSE
We can easily extract the contents of score than satisfy score<70 using logical subscripting.
score## [1] 80 65 78 94 100 NA 55 85 88 75
score[score_less_than70]## [1] 65 NA 55
We can create matrices in a number of ways. If we want to create a matrix based on an existing vector we can use the following command:
A <- matrix(data=score, nrow=10, ncol=1, byrow=FALSE, dimnames=NULL)
print(A)## [,1]
## [1,] 80
## [2,] 65
## [3,] 78
## [4,] 94
## [5,] 100
## [6,] NA
## [7,] 55
## [8,] 85
## [9,] 88
## [10,] 75
dim(A)## [1] 10 1
We can change the dimensions of the matrix, for example:
B <- matrix(data=score, nrow=5, ncol=2, byrow=FALSE, dimnames=NULL)
print(B)## [,1] [,2]
## [1,] 80 NA
## [2,] 65 55
## [3,] 78 85
## [4,] 94 88
## [5,] 100 75
dim(B)## [1] 5 2
which gives us a \(5\times 2\) matrix using byrow=FALSE option.
The transpose of a matrix can be found using t() function:
t(B)## [,1] [,2] [,3] [,4] [,5]
## [1,] 80 65 78 94 100
## [2,] NA 55 85 88 75
The position of the missing element can be accessed using
is.na(B)## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE FALSE
## [3,] FALSE FALSE
## [4,] FALSE FALSE
## [5,] FALSE FALSE
# the position of NA
which(is.na(B))## [1] 6
The position is determined column-wise. For example, consider the following
C <- t(B)
is.na(C)## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE FALSE FALSE
# the position of NA
which(is.na(C))## [1] 2
Sometimes we want to create a blank matrix to be filled later in a loop. For example, to create a matrix of zeros use
zeros <- matrix(data = 0, nrow = 5, ncol = 5)The functions cbind and rbind can be used to create rectangular arrays (matrices). For example
x <- cbind(b,a)
# also check out the following
cbind(1,1:5)## [,1] [,2]
## [1,] 1 1
## [2,] 1 2
## [3,] 1 3
## [4,] 1 4
## [5,] 1 5
Let us define another column vector that contains gender:
gender <- c("Female", "Male", "Female", "Female", "Male", "Female", "Male", "Male", "Female", "Female")
print(gender) ## [1] "Female" "Male" "Female" "Female" "Male" "Female" "Male" "Male"
## [9] "Female" "Female"
table(gender)## gender
## Female Male
## 6 4
Think of gender as a categorical variable. Each observation is assigned to a gender category.
Let us combine these two pieces of information, scores and gender, and call it x:
x<- cbind(score,gender)
x## score gender
## [1,] "80" "Female"
## [2,] "65" "Male"
## [3,] "78" "Female"
## [4,] "94" "Female"
## [5,] "100" "Male"
## [6,] NA "Female"
## [7,] "55" "Male"
## [8,] "85" "Male"
## [9,] "88" "Female"
## [10,] "75" "Female"
Note that all elements are strings (why?).
You can save these objects for later use:
save(score, gender, B, b, x, file = "../Data/myfirstRdata.RData")Reload them using:
load("../Data/myfirstRdata.RData")We combine vectors and matrices having the same dimensions into a special type of object called data frame. For example
mydata<- data.frame(score,gender)
mydata## score gender
## 1 80 Female
## 2 65 Male
## 3 78 Female
## 4 94 Female
## 5 100 Male
## 6 NA Female
## 7 55 Male
## 8 85 Male
## 9 88 Female
## 10 75 Female
names(mydata)## [1] "score" "gender"
str(mydata)## 'data.frame': 10 obs. of 2 variables:
## $ score : num 80 65 78 94 100 NA 55 85 88 75
## $ gender: chr "Female" "Male" "Female" "Female" ...
summary(mydata)## score gender
## Min. : 55 Length:10
## 1st Qu.: 75 Class :character
## Median : 80 Mode :character
## Mean : 80
## 3rd Qu.: 88
## Max. :100
## NA's :1
We will revisit data frames in the next lab session.
As we mentioned above, TRUE and FALSE are reserved words in R indicating whether a given statement is true or false. Internally, TRUE (T) is set to 1 and FALSE (F) is set to 0. Try the following commands yourself
# logical operators, ==, !=, >, <, >=, <=
0 == 0## [1] TRUE
0 == 1## [1] FALSE
a >= 3## [1] FALSE FALSE TRUE TRUE TRUE
# note what happens if you type
1-(a<=3)## [1] 0 0 0 1 1
We can create logical vectors using the function logical(), or using conditional statements on existing vectors. For example,
# logical creates a vector in which each element is zero
logical(5)## [1] FALSE FALSE FALSE FALSE FALSE
# Which observations are Male
gender=="Male"## [1] FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE
# indicate scores higher than or equal to 75
ind <- (score>=75)
cbind(score, ind)## score ind
## [1,] 80 1
## [2,] 65 0
## [3,] 78 1
## [4,] 94 1
## [5,] 100 1
## [6,] NA NA
## [7,] 55 0
## [8,] 85 1
## [9,] 88 1
## [10,] 75 1
A list is a general grouping of any kind of objects in R and it can have different types of the components.
# Generate a list object:
mylist <- list( A=seq(8,36,4), this="that", idm = diag(3))
# Print the list:
mylist## $A
## [1] 8 12 16 20 24 28 32 36
##
## $this
## [1] "that"
##
## $idm
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
In order to name the components names() command may be used.
names(mylist)## [1] "A" "this" "idm"
Also a specific component can be refered by list_name$component_name.
# Print component "A":
mylist$A## [1] 8 12 16 20 24 28 32 36
On the console type help(curve) or ?curve:
?curve# a linear function
curve(1+2*x, from=-2, to=2, xlab="x", ylab="" )
# add a quadratic function
curve(1+2*x-2*x^2, add = TRUE, col = "red" )
# cubic
curve(1+2*x-2*x^2+3*x^3, add = TRUE, col = "blue" )Another way of visualizing an algebraic function is to use function() and curve() or plot() together.
# Define the function (this is your first function here !)
fun1 <- function(x) 2*sin(x)+cos(x)
curve(fun1, -pi, pi, n = 100) # focus on the interval -2 < x < 2
plot(fun1, -2, 2)Basic R: Swirl
RStudio Beginners Starting Point
Datacamp course: Introduction to R. (assignment)