62
An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen ([email protected]) An Introduction to R October 16, 2016 1 / 26

An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen ([email protected]) An Introduction to R October 16, 2016 1 / 26

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

An Introduction to R

Instructor: Xi Chen

October 16, 2016

Xi Chen ([email protected]) An Introduction to R October 16, 2016 1 / 26

Page 2: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Outline

1 A short introduction

Xi Chen ([email protected]) An Introduction to R October 16, 2016 2 / 26

Page 3: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 4: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 5: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 6: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 7: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 8: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 9: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Starting with R

Obtaining R from http://www.R-project.org.

Installing add-on packages

”Packages→ Install package from CRAN. . .”

install.packages(”RMySQL”)

installed.packages()

old.packages()

update.packages()

Xi Chen ([email protected]) An Introduction to R October 16, 2016 3 / 26

Page 10: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Getting Help in R

HELP menu

help.start()

help() function: e.g. help(plot), ?plot

Xi Chen ([email protected]) An Introduction to R October 16, 2016 4 / 26

Page 11: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Getting Help in R

HELP menu

help.start()

help() function: e.g. help(plot), ?plot

Xi Chen ([email protected]) An Introduction to R October 16, 2016 4 / 26

Page 12: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Getting Help in R

HELP menu

help.start()

help() function: e.g. help(plot), ?plot

Xi Chen ([email protected]) An Introduction to R October 16, 2016 4 / 26

Page 13: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 14: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 15: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 16: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 17: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 18: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

assignment operator “<–”

Example 1 (assignment operator)e.g. y<–39; z<–5try w<–z2 and (w<–z2)

Ex. Suppose z = 5 and i = z2+452 , what is the value of i ?

listing and deleting objects

ls() or objects, rm()

try ls(), rm(y), rm (z,w,i)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 5 / 26

Page 19: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 20: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 21: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 22: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 23: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 24: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

R Objects

valid object names

upper and lower-case letters

the digits 0− 9 (except in the beginning of the name)

period “.” (cannot appear at the beginning of the name )

case sensitive: “Color” and “color” are two distinct objects.

you are not allowed to use the underline “’’ in object names.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 6 / 26

Page 25: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

mode: character, logical, numeric, or complex

length: length()

c(): creating a vector

Example 2 (creating a vector)e.g. v<-c(4,7,23.5,76.2,80)try length(v), mode(v)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 7 / 26

Page 26: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

mode: character, logical, numeric, or complex

length: length()

c(): creating a vector

Example 2 (creating a vector)e.g. v<-c(4,7,23.5,76.2,80)try length(v), mode(v)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 7 / 26

Page 27: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

mode: character, logical, numeric, or complex

length: length()

c(): creating a vector

Example 2 (creating a vector)e.g. v<-c(4,7,23.5,76.2,80)try length(v), mode(v)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 7 / 26

Page 28: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

mode: character, logical, numeric, or complex

length: length()

c(): creating a vector

Example 2 (creating a vector)e.g. v<-c(4,7,23.5,76.2,80)try length(v), mode(v)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 7 / 26

Page 29: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

all elements of a vector must belong to the same mode

NA: missing values. e.g. v<-c(4,6,NA,2)

accessing individual elements through an index. try v[2]

Example 3 (changing, adding and removing elements)try v[1]<-9, v<-vector(), v[3]<-45try v<-c(v[1],v[3])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 8 / 26

Page 30: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

all elements of a vector must belong to the same mode

NA: missing values. e.g. v<-c(4,6,NA,2)

accessing individual elements through an index. try v[2]

Example 3 (changing, adding and removing elements)try v[1]<-9, v<-vector(), v[3]<-45try v<-c(v[1],v[3])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 8 / 26

Page 31: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

all elements of a vector must belong to the same mode

NA: missing values. e.g. v<-c(4,6,NA,2)

accessing individual elements through an index. try v[2]

Example 3 (changing, adding and removing elements)try v[1]<-9, v<-vector(), v[3]<-45try v<-c(v[1],v[3])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 8 / 26

Page 32: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectors

all elements of a vector must belong to the same mode

NA: missing values. e.g. v<-c(4,6,NA,2)

accessing individual elements through an index. try v[2]

Example 3 (changing, adding and removing elements)try v[1]<-9, v<-vector(), v[3]<-45try v<-c(v[1],v[3])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 8 / 26

Page 33: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectorization

Example 4Try to calculate the square root of 4, 7, 23.5, 76.2, 80. (hint: sqrt())

Example 5 (vector arithmetic)v1<-c(4,6,8),v2<-c(34,32.4,12), try v1+v2

The recycling rule: If vectors do not have the same length, R willrepeat the shorter vector till it fills in the size of the larger.

Example 6 (recycling rule)Suppose v1=(4,6,8,24),v2=(10,2),v3=(10,2,4), try v1+v2, v1+v3.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 9 / 26

Page 34: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectorization

Example 4Try to calculate the square root of 4, 7, 23.5, 76.2, 80. (hint: sqrt())

Example 5 (vector arithmetic)v1<-c(4,6,8),v2<-c(34,32.4,12), try v1+v2

The recycling rule: If vectors do not have the same length, R willrepeat the shorter vector till it fills in the size of the larger.

Example 6 (recycling rule)Suppose v1=(4,6,8,24),v2=(10,2),v3=(10,2,4), try v1+v2, v1+v3.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 9 / 26

Page 35: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectorization

Example 4Try to calculate the square root of 4, 7, 23.5, 76.2, 80. (hint: sqrt())

Example 5 (vector arithmetic)v1<-c(4,6,8),v2<-c(34,32.4,12), try v1+v2

The recycling rule: If vectors do not have the same length, R willrepeat the shorter vector till it fills in the size of the larger.

Example 6 (recycling rule)Suppose v1=(4,6,8,24),v2=(10,2),v3=(10,2,4), try v1+v2, v1+v3.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 9 / 26

Page 36: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Vectorization

Example 4Try to calculate the square root of 4, 7, 23.5, 76.2, 80. (hint: sqrt())

Example 5 (vector arithmetic)v1<-c(4,6,8),v2<-c(34,32.4,12), try v1+v2

The recycling rule: If vectors do not have the same length, R willrepeat the shorter vector till it fills in the size of the larger.

Example 6 (recycling rule)Suppose v1=(4,6,8,24),v2=(10,2),v3=(10,2,4), try v1+v2, v1+v3.

Xi Chen ([email protected]) An Introduction to R October 16, 2016 9 / 26

Page 37: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Factors

handling categorical (nominal) data

levels: possible value they may take

creating a factor: g<-factor()

Example 7 (creating a factor)g<-c(‘f’, ‘m’, ‘m’, ‘m’, ‘f’, ‘m’, ‘f’, ‘m’, ‘f’, ‘f’)g<-factor(g) ← numeric vectorother.g<-factor(c( ‘m’, ‘m’, ‘m’, ‘m’, ‘m’), levels=c(‘f’, ‘m’))other.g

Xi Chen ([email protected]) An Introduction to R October 16, 2016 10 / 26

Page 38: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Factors

handling categorical (nominal) data

levels: possible value they may take

creating a factor: g<-factor()

Example 7 (creating a factor)g<-c(‘f’, ‘m’, ‘m’, ‘m’, ‘f’, ‘m’, ‘f’, ‘m’, ‘f’, ‘f’)g<-factor(g) ← numeric vectorother.g<-factor(c( ‘m’, ‘m’, ‘m’, ‘m’, ‘m’), levels=c(‘f’, ‘m’))other.g

Xi Chen ([email protected]) An Introduction to R October 16, 2016 10 / 26

Page 39: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Factors

handling categorical (nominal) data

levels: possible value they may take

creating a factor: g<-factor()

Example 7 (creating a factor)g<-c(‘f’, ‘m’, ‘m’, ‘m’, ‘f’, ‘m’, ‘f’, ‘m’, ‘f’, ‘f’)g<-factor(g) ← numeric vectorother.g<-factor(c( ‘m’, ‘m’, ‘m’, ‘m’, ‘m’), levels=c(‘f’, ‘m’))other.g

Xi Chen ([email protected]) An Introduction to R October 16, 2016 10 / 26

Page 40: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Factors

frequency tables for factors: table(g); table(other.g)

Example 8 (cross tabulation)a<-factor(c(‘adult’,‘adult’,‘juvenile’, ‘juvenile’, ‘adult’,‘adult’,‘adult’,‘juvenile’,‘adult’, ‘juvenile’))table(a,g)

Example 9 (marginal and relative frequencies)t<-table(a,g)margin.table(t,1); margin.table(t,2)prop.table(t,1); prop.table(t,2); prop.table(t)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 11 / 26

Page 41: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Generating Sequences

Example 10 (integer sequences)x<-1:1000; y<-5:0try 10:15-1 and 10:(15-1)

Example 11 (sequences of real numbers)seq(-4,1,0.5); seq(from=1,to=5,length=4)seq(length=10,from=-2,by=0.2); seq(from=1,by=-5,length=2,)

Example 12 (sequences with repeated elements)rep(5,10); rep(1:3,2)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 12 / 26

Page 42: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Generating Sequences

Example 13 (factor sequences )gl(k,n): gl(3,5)gl(2,5, labels=c(‘female’,‘male’))

Example 14 (random sequences)rfunc(n,par1,par2,. . .)rnorm(10); rnorm(10,mean=10,sd=3); rt(5,df=10)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 13 / 26

Page 43: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Indexing

Example 15 (logical index vectors)x<-c(0,3,4,-1,45,90,-5)x>0; y<-x>0try x[y] and x[x>0]try to get elements in x that satisfies (1) xi < −2 or xI > 5; (2)40 < xi < 100.

Example 16 (integer index vectors)x<-c(0,3,4,-1,45,90,-5)x[c(4,6)]; x[1:3]; x[-1]; x[-c(4,6)]

Xi Chen ([email protected]) An Introduction to R October 16, 2016 14 / 26

Page 44: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Indexing

Example 17 (character string index vectors)pH<-c(4.5,7,7.3,8.2,6.3)names(pH)<-c(‘area1’,‘area2’,‘mud’,‘dam’,‘middle’)try pH and pH[‘mud’]try to get the pH value of areal1 and dam.

Example 18 (empty index)all elements are selectedtry x[ ]<-0 and x<-0

Xi Chen ([email protected]) An Introduction to R October 16, 2016 15 / 26

Page 45: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Matrices and Arrays

arrays arrange data elements in several dimensions

matrices are a special case of arrays with two single dimensions

Example 19 (matrices and arrays)Suppose you have the vector of numbersm=(45,23,66,77,33,44,56,12,78,23), convert it into a 2-by-5 matrix.

m<-c(45,23,66,77,33,44,56,12,78,23)dim(m)<-c(2,5)

Example 20 (creating a matrix)m<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5) ← fill by columnm<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5, byrow=T) ← fill byrow

Xi Chen ([email protected]) An Introduction to R October 16, 2016 16 / 26

Page 46: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Matrices and Arrays

arrays arrange data elements in several dimensions

matrices are a special case of arrays with two single dimensions

Example 19 (matrices and arrays)Suppose you have the vector of numbersm=(45,23,66,77,33,44,56,12,78,23), convert it into a 2-by-5 matrix.

m<-c(45,23,66,77,33,44,56,12,78,23)dim(m)<-c(2,5)

Example 20 (creating a matrix)m<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5) ← fill by columnm<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5, byrow=T) ← fill byrow

Xi Chen ([email protected]) An Introduction to R October 16, 2016 16 / 26

Page 47: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Matrices and Arrays

arrays arrange data elements in several dimensions

matrices are a special case of arrays with two single dimensions

Example 19 (matrices and arrays)Suppose you have the vector of numbersm=(45,23,66,77,33,44,56,12,78,23), convert it into a 2-by-5 matrix.

m<-c(45,23,66,77,33,44,56,12,78,23)dim(m)<-c(2,5)

Example 20 (creating a matrix)m<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5) ← fill by columnm<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5, byrow=T) ← fill byrow

Xi Chen ([email protected]) An Introduction to R October 16, 2016 16 / 26

Page 48: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Matrices and Arrays

arrays arrange data elements in several dimensions

matrices are a special case of arrays with two single dimensions

Example 19 (matrices and arrays)Suppose you have the vector of numbersm=(45,23,66,77,33,44,56,12,78,23), convert it into a 2-by-5 matrix.

m<-c(45,23,66,77,33,44,56,12,78,23)dim(m)<-c(2,5)

Example 20 (creating a matrix)m<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5) ← fill by columnm<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5, byrow=T) ← fill byrow

Xi Chen ([email protected]) An Introduction to R October 16, 2016 16 / 26

Page 49: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Matrices and Arrays

arrays arrange data elements in several dimensions

matrices are a special case of arrays with two single dimensions

Example 19 (matrices and arrays)Suppose you have the vector of numbersm=(45,23,66,77,33,44,56,12,78,23), convert it into a 2-by-5 matrix.

m<-c(45,23,66,77,33,44,56,12,78,23)dim(m)<-c(2,5)

Example 20 (creating a matrix)m<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5) ← fill by columnm<-matrix(c(45,23,66,77,33,44,56,12,78,23),2,5, byrow=T) ← fill byrow

Xi Chen ([email protected]) An Introduction to R October 16, 2016 16 / 26

Page 50: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Example 21 (accessing matrix elements)m[2,2]; m[-2,2]; m[1,-c(3,5)]m[1,]; m[,4]

Example 22 (joining vectors and matrices)cbind(); rbind()cbind(c(4,76),m1[,4])Try to generate a 10-by-5 matrix m2 with each element equals to 10,then create a matrix m3 by combining the first row of m1 and thefifth row of m2.m2<-matrix(rep(10,50), 10, 5); m3<-rbind(m1[1,], m2[5,])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 17 / 26

Page 51: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Example 21 (accessing matrix elements)m[2,2]; m[-2,2]; m[1,-c(3,5)]m[1,]; m[,4]

Example 22 (joining vectors and matrices)cbind(); rbind()cbind(c(4,76),m1[,4])Try to generate a 10-by-5 matrix m2 with each element equals to 10,then create a matrix m3 by combining the first row of m1 and thefifth row of m2.m2<-matrix(rep(10,50), 10, 5); m3<-rbind(m1[1,], m2[5,])

Xi Chen ([email protected]) An Introduction to R October 16, 2016 17 / 26

Page 52: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Arrays

Example 23 (creating an array)array()a<-array(1:50,dim=(2,5,5))

Example 24 (accessing elements of arrays)a[1,5,2]; a[1,4]; a[1,3, ]; a[ ,c(3,4),-4]

Xi Chen ([email protected]) An Introduction to R October 16, 2016 18 / 26

Page 53: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Arrays

Example 25 (recycling rules also apply to matrices and arrays)

Try to create m =

(45 66 33 56 7823 77 44 12 23

),

m1 =

(45 66 3323 77 44

), and m2 =

(12 32 465 7 78

),

then calculate 3×m,m1 + m2,m + m1

Xi Chen ([email protected]) An Introduction to R October 16, 2016 19 / 26

Page 54: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Lists

an ordered collection of other objects known as its components.components do not need to be of the same type, mode or length.

Example 26 (creating a list)my.lst<-list(stud.id=34453, stud.name=“John”, stud.marks=c(14.3,12, 15,19))

Example 27 (extracting elements of a list)my.lst[[1]]try to get the marks of Johntry my.lst[1]my.lst$stud.idnames(my.lst)names(my.lst)<-c(‘id’, ‘name’, ‘marks’)

Xi Chen ([email protected]) An Introduction to R October 16, 2016 20 / 26

Page 55: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Lists

Example 28 (extending lists)my.lst$parents.names<-c(“Ana”, “Mike”)

Example 29 (concatenating lists)c()Try to create a new list “other” with components (age=19) and(sex=male), then concatenate my.lst and other

Xi Chen ([email protected]) An Introduction to R October 16, 2016 21 / 26

Page 56: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Lists

Example 28 (extending lists)my.lst$parents.names<-c(“Ana”, “Mike”)

Example 29 (concatenating lists)c()Try to create a new list “other” with components (age=19) and(sex=male), then concatenate my.lst and other

Xi Chen ([email protected]) An Introduction to R October 16, 2016 21 / 26

Page 57: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Data Frames

similar to a matrix but with named columnsit may include data of different type on each columna special class of lists

Example 30 (creating a data frame)my.dataset<-data.frame(site=c(‘A’,‘B’,‘A’,‘A’,‘B’),season=c(‘Winter’,‘Summer’,‘Summer’,‘Spring’, ‘Fall’),pH=c(7.4,6.3,8.6,7.2,8.9))my.dataset[3,2]my.dataset$pH

Xi Chen ([email protected]) An Introduction to R October 16, 2016 22 / 26

Page 58: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Data Frames

similar to a matrix but with named columnsit may include data of different type on each columna special class of lists

Example 30 (creating a data frame)my.dataset<-data.frame(site=c(‘A’,‘B’,‘A’,‘A’,‘B’),season=c(‘Winter’,‘Summer’,‘Summer’,‘Spring’, ‘Fall’),pH=c(7.4,6.3,8.6,7.2,8.9))my.dataset[3,2]my.dataset$pH

Xi Chen ([email protected]) An Introduction to R October 16, 2016 22 / 26

Page 59: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Data Frames

Example 31 (Querying data frames)my.dataset[my.dataset$pH>7]my.dataset[my.dataset$site=‘A’, ‘pH’]show the site and pH value of entries with season=Summer.attach( ); detach()attach(my.dataset); my.dataset[pH>8, ]; season

Xi Chen ([email protected]) An Introduction to R October 16, 2016 23 / 26

Page 60: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Data Frames

Example 32 (adding columns to a data frame)my.dataset$NO3<-c(234.5,256.6,654.1,356.7,776.4); my.dataset$NO3new columns must have the same number of rows as the existing dataframe.nrow(my.dataset); ncol(my.dataset)names(my.dataset);names(my.dataset)<-c(“area”,“season”,“pH”,“NO3”);names(my.dataset)[4]<-“PO4”

Xi Chen ([email protected]) An Introduction to R October 16, 2016 24 / 26

Page 61: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Creating New Functions

Example 33

standard error=√

s2n , where s2 is the variance and n is the sample size

Try to check whether there is already a function with name ‘se’ in Rsese<-funcion(x){v<-var(x); n<-length(x); return(sqrt(v/n))}calculate the standard deviation of vector (45,2,3,5,76,2,4)function(<list of parameters>){<list of R instructions>}

Xi Chen ([email protected]) An Introduction to R October 16, 2016 25 / 26

Page 62: An Introduction to R · An Introduction to R Instructor: Xi Chen October 16, 2016 Xi Chen (chenxi0109@bfsu.edu.cn) An Introduction to R October 16, 2016 1 / 26

A short introduction

Thank You

Xi Chen ([email protected]) An Introduction to R October 16, 2016 26 / 26