1.1.4 Introduction To R FW
1.1.4 Introduction To R FW
1.1.4 Introduction To R FW
Statistical Learning
Introduction
Lesson 4: Introduction to R
Learning Objectives
• Execute Installation of R
• Manipulate Data With R
• Review Programming
What is R
• R is a free software for statistical computation and
graphics
• Homepage: http://www.r-project.org
Installing R Under Windows
Suppose you have a laptop with Windows OS(32/64 bits), how to install
R in your laptop?
• Go to any CRAN site (see http://cran.r-project.org for a list), and click
“Download R for Windows.”
• Follow the instruction to click ‘install R for the first time”
• Download R installation file, double-click on the icon and follow the
instructions to install.
As of January 2020, the file is “R-3.6.2-win.exe” (Size: 83Mb).
Data With R
• Objects: vector, matrix, data.frame, ts, list, factor, array
• Mode (numerical, character, complex, and logical);Length
• Read data stored in text (ASCII) files
read.table(), scan(), and read.fwf()
• Saving data
write(x, file=“data.txt”), write.table() write in a file a data.frame
Generating Data in R
# Generating data
x1 <- 1:10 # x1 = c(1,2,3,4,5,6,7,8,9,10)
x2 <- seq(1,4,0.5) # x2 = c(1.0,1.5, 2.0, 2.5, 3.0, 3.5, 4.0)
x3 <- rep(1,5) # x3 = c(1,1,1,1,1)
x4 <- gl(2,3,10) (generate factor levels)
#Check x4 values in R
> x4
[1] 1 1 1 2 2 2 1 1 1 2
Levels: 1 2
Matrix in R
# matrix in R
y <- matrix(c(1,2,3,4,5,6),2,3)
# This is 2x3 Matrix filled in column by column
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
# Plots
plot(x,y); abline(fm1); abline(fm2, col=“red”)
Programming Language
• You can write your own function in R, and then call that function later.
• R is rather slow in loops, and you should avoid loops as much as
possible by using matrix operations.
• Books related to R:
https://www.r-project.org/doc/bib/R-books.html
• R Installation
• Data with R
• Programming in R