LEC. 1: INTRODUCTION TO R PROGRAMMING -PART I
Instructor: SANG-HOON CHO
DEPT. OF STATISTICS AND ACTUARIAL SCIENCES
Soongsil University
1 / 27
1. Getting Started
Why use R?
Public domain (a so called GNU) project
Implementation of the commercial S language and environment developed at Bell Lab. byJohn Chambers and colleagues
Freeware and lots of help available online
Powerful language and environment for statistical computing and graphics
User-friendly and Objective Oriented Programming(OOP)
Popular for big data analysis
2 / 27
3 / 27
1. Getting Started
Installation of R
https://www.r-project.org
4 / 27
1. Getting Started
Installation of RStudio
https://www.rstudio.com
5 / 27
1. Getting Started
RStudio Layout
NOTE: Ctrl + R or Ctrl + Enter
pass lines or highlighted blocks from Editor Window to Console Window
6 / 27
1. Getting Started
R Version
R.Version(), version, getRversion()
> R.Version()$version.string[1] "R version 3.3.1 (2016-06-21)"> version
_platform x86_64-apple-darwin13.4.0arch x86_64os darwin13.4.0system x86_64, darwin13.4.0statusmajor 3minor 3.1year 2016month 06day 21svn rev 70800language Rversion.string R version 3.3.1 (2016-06-21)nickname Bug in Your Hair> getRversion()[1] 3.3.1
7 / 27
1. Getting Started
Working Directory
getwd(), setwd(), dir()Your working directory is the folder on your computer in which you are currently working.
When you ask R to open a certain file, it will look in the working directory for this file, and when you tell R to save
a data file or figure, it will save it in the working directory.
Before you start working, set your working directory to where all your data and script files are or should be stored.
> getwd() # checking current working directory[1] "/Users/SHCHO"> setwd("/Users/SHCHO/Desktop/R/") # setting current working directory> dir() # print files in the current working directory
Can use a hasmark (#) for a comment
8 / 27
2. Some First Examples of R Commands
R as a calculator
102 +e2 +sin(π)+36
log10(100)+ log2(4)+ ln(e2)
√100+
√4
1/1001/2
> 10ˆ2 + exp(2) + sin(pi) + 36[1] 143.3891> log10(100) + log2(4) + log(exp(2))[1] 6> sqrt(100) + sqrt(4)[1] 12> (1/100)/(1/2)[1] 0.02
9 / 27
2. Some First Examples of R Commands
Objects & Workspace
objects()Can save a calculated result as a variable and use it later
> res1 <- 10ˆ2 + exp(2) + sin(pi) + 36 # "<-" means assign. can be replaced by "="> res1[1] 143.3891> objects()[1] "res1"
Can see that res1 appears in Workspace Window
> res2 <- log10(100) + log2(4) + log(exp(2))> res2[1] 6> res3 <- sqrt(100) + sqrt(4)> res3[1] 12> objects()[1] "res1" "res2" "res3"
Can see that res2 and res3 appear as well in Workspace Window
10 / 27
2. Some First Examples of R Commands
Objects & Workspace
Can calculate a new value using res1
If assign a new value to res1 again, it will forget what value it had before
> res1[1] 143.3891> res1 * 50[1] 7169.453> res1 <- res1 + 50> res1[1] 193.3891
11 / 27
2. Some First Examples of R Commands
Objects & Workspace
rm()Can clear some or all variables in Workspace Window
> rm(res1, res2)> objects()[1] "res3"> rm(list=ls())> objects()character(0)
12 / 27
2. Some First Examples of R Commands
Objects & Workspace
R is an expression language with a very simple syntax
It is case sensitive so A and a are different symbols and would refer to different variables
> A <- 1:10> a <- 2:11> A[1] 1 2 3 4 5 6 7 8 9 10
> a[1] 2 3 4 5 6 7 8 9 10 11
Commands are separated either by a semi-colon (;) or a newline
> A; a[1] 1 2 3 4 5 6 7 8 9 10[1] 2 3 4 5 6 7 8 9 10 11
13 / 27
2. Some First Examples of R Commands
Help & Documentation
help(),?There is a very useful help facility in R that you can learn R functions by yourselves
> help(var) # help facility invoked by the function "help"> ?var # a faster alternative> ?sin> ?sqrt
For a feature specified by special characters, the argument must be enclosed in double or single quotes
> help("?") # Read the help page about how to use help> ?’?’ # a faster alternative> help(">")starting httpd help server ... done
14 / 27
3. R Objects and Classes
Objects
Everything in R is an object
Every object in R has a class
The entities R operates on are technically known as objects
Data, intermediate results, functions, and even the results of a regression fit are stored as objects
15 / 27
3. R Objects and Classes
Types of Objects
vectors: a single entity consisting of an ordered collection of numbers
matrices or arrays: multi-dimensional generalizations of vectors
factors: sets of labelled observations with a pre-defined set of labels
lists: a vector of R objects
data frames: matrix-like structures in which the columns can be of different types
functions
16 / 27
3. R Objects and Classes
Vectors and Classes (Modes)
The most important type of objects in R is a vector
Vectors are known as atomic structures, i.e., their components are all of the same type
The most common classes (modes) of vectors are
character: a vector of character strings of varying (and unlimited) length
numeric: a vetcor of real numbers
integer: a vectcor of (signed) integers
logical: a vector of logical (true or false) values
complex: a vector of complex numbers
list: a vector of R objects
17 / 27
3. R Objects and Classes
Vectors: 1. Numeric
c()A simple way to construct a vector is to use the c (combine) function
> c(1,2,3,4)[1] 1 2 3 4> rev(c(1,2,3,4)) # reverse the order of elements in the vector[1] 4 3 2 1
:The sequence operator : also generates consecutive numbers
> 1:4[1] 1 2 3 4> 4:1[1] 4 3 2 1> -1:2[1] -1 0 1 2
18 / 27
3. R Objects and Classes
Vectors: 1. Numeric
seq()seq is the main function for generating numeric vectors
> seq(2, 8, by = 2) # specify an interval[1] 2 4 6 8> seq(0, 1, by = 0.1)[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
> seq(0, 1, length = 11) # specify the number of elements[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
19 / 27
3. R Objects and Classes
Vectors: 1. Numeric
The standard arithmetic functions and operators apply to vectors on an element-wise basis
> c(1,2,3,4)/2[1] 0.5 1.0 1.5 2.0> c(1,2,3,4)/4:1[1] 0.2500000 0.6666667 1.5000000 4.0000000> log( c(0.1, 1, 10, 100), 10 )[1] -1 0 1 2
20 / 27
3. R Objects and Classes
Vectors: 1. Numeric
Recycling Rule
If the operands are of different lengths, then the shorter of the two is extended by repetition
> c(1,2,3,4) + c(4,3)[1] 5 5 7 7
21 / 27
3. R Objects and Classes
Vectors: 1. Numeric
Indexing
Indices are specified between square brackets [ ]
> v <- 10:1; v[1] 10 9 8 7 6 5 4 3 2 1
> v[2][1] 9> v[c(4,2,6)][1] 7 9 5> v[c(4,2,4)][1] 7 9 7> v[-c(2,4,5,8,10)][1] 10 8 5 4 2
22 / 27
3. R Objects and Classes
Vectors: 2. Logical
Logical Operators
Logical vectors can be created by using logical operators: &, |, !, ==, !=, <=,<,>,>=
> x <- seq(0, 10, by = 0.5); x[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
[18] 8.5 9.0 9.5 10.0> y <- x >= 5; y[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
[15] TRUE TRUE TRUE TRUE TRUE TRUE TRUE> z <- x == 6; z[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[15] FALSE FALSE FALSE FALSE FALSE FALSE FALSE> which(z)[1] 13
23 / 27
3. R Objects and Classes
Vectors: 2. Logical
Indexing
Indices are specified between square brackets [ ]
> x[y][1] 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
> x[which(z)][1] 6> v <- 10:1; v[1] 10 9 8 7 6 5 4 3 2 1
> v[v < 6][1] 5 4 3 2 1> v[ v <= 3 | v >= 9 ][1] 10 9 3 2 1> v[ v <= 3 & v >= 9 ]integer(0)> z <- -10:10> z[abs(z) <= 3][1] -3 -2 -1 0 1 2 3
24 / 27
3. R Objects and Classes
Vectors: 3. Character
> names <- c("John","Georges","Mary"); names[1] "John" "Georges" "Mary"> letters[1:5][1] "a" "b" "c" "d" "e"> LETTERS[1:5][1] "A" "B" "C" "D" "E"> state.name[10:15][1] "Georgia" "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa"
25 / 27
3. R Objects and Classes
Vectors
rep()Another way to construct any type of vectors is to use the rep function
> rep(5,3)[1] 5 5 5> rep( c(1,2,3), 2 )[1] 1 2 3 1 2 3> rep(1:3, 3:1)[1] 1 1 1 2 2 3> rep( c("Rep", "Dem"), c(10,10) )[1] "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Rep" "Dem" "Dem" "Dem" "Dem"
[15] "Dem" "Dem" "Dem" "Dem" "Dem" "Dem"
26 / 27
3. R Objects and Classes
Vectors
mode(), length()For any object of R, you can apply two functions: mode and length
> mode(v)[1] "numeric"> length(v)[1] 10>> mode(y)[1] "logical"> length(y)[1] 21>> mode(names)[1] "character"> length(names)[1] 3
27 / 27