Introduction to R
Learning Outcomes
● What is R?
● R vs. RStudio
● Getting familiarize with R
● Basic Data Types
● Vectors
● Matrix
● List
● Dataframe
● Working with the Data: An Application
Introduction to R
What is R? Why R?
● Language for Statistical Computing and Graphics
○ Developed in Auckland, NZ
R vs. Rstudio
R RStudio
Familiarize with R
Console Input/Command
Output
# 6 mod 8, gives you the remainder when 6 is divided by 8
Variable
● Assign almost anything to a variable by
using either <- or =
● Use it later on
Workspace
● Must start with an alphabet
Ext. .RData
● Can only contain
“Numbers”,”Alphabets”,”Underline
Character”, “dot”
● Reserved words can’t be assigned as
variable name
● R is case sensitive !
Introduction to R
Familiarize with R
R script : Text (“.txt”) file containing R commands
Extension .R
Introduction to R
● Numeric
○ Integer
● Character/String
Vectors
● Automatic coercion
Creating a Vector
● A single value is a vector ● 1:9 is same as c(1,2,3,...,9)
● Even missing value NA ! ● seq(x,y,z) generates sequence
● Every vector in R is a row vector by default! ○ lower limit x
○ upper limit y
● Rows: Data points/Observation ○ with increments z (default = 1)
● Column: a variable of data
Introduction to R
Coercion
● R performs automatic conversion of data types
■ c(“A”, 8) => c(“A”, “8”) Numerics gets converted into characters
■ c(T, 0 , 1) => c(1, 0, 1) Logicals gets converted into Numerics
Introduction to R
Logical Test
● is.vector(x) :to check if a variable/data point x is vector or not
Introduction to R
Logical Test
Introduction to R
Naming a Vector
● Two ways to name the elements:
○ names() function
○ Use names of each element while creating a vector
Introduction to R
Vector Calculus
● Vector operations are done element wise
■ +
■ -
■ *
■ /
■ ^ : To exponentiate
■ log() : computes natural logarithm function ie base e
■ exp() : natural exponential function
■ %% : modulus operator, returns remainder
Vector Calculus
sum(x) : Computes sum of all elements of x
Introduction to R
Subsetting Vector
Square brackets []
● Index number
vector
● (-) minus sign to
exclude values
that aren’t
required
● Logicals: T, F
● Conditioning
Introduction to R
Matrix
● Vector: 1D array of data elements
● Matrix: 2D array of data elements
○ Rows and Columns
● Accepts only same data type, Coercion occurs otherwise
● dim(x): Retrieves and sets dimensions of x
Introduction to R
Creating a Matrix
● matrix(data = , nrow = , ncol = , byrow = )
Introduction to R
Naming a Matrix
● rownames(m) : retrieve or set the names of the rows of matrix m
● colnames(m): retrieve or set the names of the columns of matrix m
Introduction to R
Matrix Calculus
Introduction to R
Matrix Subsetting
● Subsetting by row or a column
○ PXY[2, ] : entire row 2 of matrix PXY
○ PXY[ ,1] : entire column 1 of matrix PXY
● Subsetting by names
○ PXY[“Mon”, “Y”] : Profit made on asset Y on monday
○ PXY[“Tue”, ] : Profit made on asset X and asset Y on tuesday
● Subsetting multiple entries
○ PXY[c(2, 3), -1]: Profit made on asset Y on tuesday and wednesday
● Subsetting by logicals
○ PXY[c(TRUE, FALSE), ] : Profit matrix for alternate days
Introduction to R
● PXY[PXY[ , “X”] > 0 & PXY[ , “Y”] > 0, ]: Profit matrix with strictly positive profit on both
the assets
Introduction to R
Data Frame
● Specifically for data sets
● Observations : Rows
● Variables : Columns
● Elements of same column : Same type!
● data.frame(): Creates data frame
Introduction to R
● Using $
● Using subset()
Working Directory
● All the work gets saved in the working directory
Application
● Statistics & Econometrics
● Mathematics
● Finance
Introduction to R
Application : Statistics
● Descriptive Statistics
● Plotting data
Plotting Symbols
Source: Quick-R
Introduction to R
Source: Quick-R
Introduction to R
Application : Econometrics
● Linear Regression
● ANOVA table
Application : Mathematics
● Determinant of a Matrix
Dates in R
● Date: Another type of data
Dates in R
as.Date(x, format = )
○ 24/09/2015: as.Date(‘24/09/2015’,
‘%d/%m/%Y’)
■ “2015-09-24”
○ Sep 24, 2015 : as.Date(“Sep 24, 2015”, “%b %d,
%Y”)
■ “2015-09-24”
○ 2015-09-24: as.Date(“2015-09-24”)
○ 2015-09-24: as.Date(“2015-09-24”, format = “
%Y-%m-%d ”)
■ “2015-09-24”
Introduction to R
Learn More at
● edx
● Coursera
● DataCamp