Top Banner
R basics workshop J. Sebastián Tello Iván Jiménez Center for Conservation and Sustainable Development Missouri Botanical Garden
42

R basics workshop

Jan 21, 2016

Download

Documents

rozene

R basics workshop. J. Sebasti án Tello Iván Jiménez. Center for Conservation and Sustainable Development Missouri Botanical Garden. 3 . Objects. Object are mainly used to hold data. In R, an object is a pointer to a piece of memory that contains some information - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: R basics workshop

R basics workshopJ. Sebastián TelloIván JiménezCenter for Conservation and Sustainable DevelopmentMissouri Botanical Garden

Page 2: R basics workshop

3. Objects

Page 3: R basics workshop

Object are mainly used to hold data

• In R, an object is a pointer to a piece of memory that contains some information

• One of the main uses for objects is to hold data

• There are many different classes of objects each with its own properties and uses

Page 4: R basics workshop

• If we counted the number of individuals of tree species in a plot, we can concatenate the values with the function c:

Objects

c(1, 17, 34, 26, 82)

Page 5: R basics workshop

Objects are frequently created with the operator “<-”

abund <- c(1, 17, 34, 26, 82)

abund

• These values could be stored in an object with “any” name using the operator “<-”:

Page 6: R basics workshop

Objects can be created other ways too

• The same operation can also be like this:

c(1, 17, 34, 26, 82) -> abund2

abund4 = c(1, 17, 34, 26, 82)

assign(x="abund3", value=c(1, 17, 34, 26, 82))

abundabund2abund3abund4

identical(abund, abund2)

Page 7: R basics workshop

Data in objects can be of various types

abund <- c(1, 17, 34, 26, 82)

mode(x=abund)

mode(abund)

1. Numeric: E.g., number of individuals per species

Page 8: R basics workshop

spp <- c("I.ynga", "I.edulis", "I.macrophylla", "I.punctata", "I.alba")

spp

mode(spp)

1. Numeric: E.g., number of individuals per species

2. Character: E.g., names of species

Data in objects can be of various types

Page 9: R basics workshop

1. Numeric: E.g., number of individuals per species

2. Character: E.g., names of species

3. Logical (true/false): E.g., increment in abundance?

increm <- c(TRUE, FALSE, FALSE, TRUE, TRUE)

increm

mode(increm)

Data in objects can be of various types

Page 10: R basics workshop

Special values

1. NA: missing value; not available; not applicable

2. Inf and -Inf : infinite

3. NaN: not a number

4. NULL: object missing

100/0

-100/0

100 - Inf

Inf - Inf

Page 11: R basics workshop

An object in R is similar to an objeto in the real world

Class

Tree(In the real world)

HeightDBH

Leaf shape

Other attributes

• Objects have attributes which define their properties

• Class is one of the main attributes and it helps determine others

matrix(In R)

Number of rowsNumber of columns

Page 12: R basics workshop

Basic classes of R objects

Class Type of data it holds Various types of data possible?

numeric (vector) numeric No

character (vector) character No

logical (vector) logical No

matrix numeric, character or logical No

array numeric, character or logical No

factor numeric or character No

data.frame numeric, character and/or logical Yes

list numeric, character and/or logical Yes

Page 13: R basics workshop

• Vectors represent a linear sequence of values, e.g.:

sp_1 sp_2 sp_3 sp_4 sp_51 17 34 26 82

Basic classes of R objects – The Vector

etc…

Value in first position

Value in second position

Page 14: R basics workshop

abund <- c(1, 17, 34, 26, 82)class(abund)

• Length is an important attribute of vectors:

length(x=abund)

Basic classes of R objects – Numeric vectors

sp_1 sp_2 sp_3 sp_4 sp_5

1 17 34 26 82

First position Fifth position

Page 15: R basics workshop

• Another one is the names of positions in the vector:

names(x=abund)

names(abund) <- paste("sp", seq(1,length(abund)), sep="")

names(abund)

abund

Basic classes of R objects – Numeric vectors

Names of positions

sp1 sp2 sp3 sp4 sp5

1 17 34 26 82

Page 16: R basics workshop

spp <- c("I.ynga", "I.edulis", "I.macrophylla", "I.punctata", "I.alba")

class(spp)

length(spp)

sp_1 sp_2 sp_3 sp_4 sp_5

I.ynga I.edulis I.macrophylla I.punctata I.alba

Basic classes of R objects – Character vectors

Page 17: R basics workshop

names(abund)

names(abund) <- spp

abund

• We can use one vector to assign names to the other:

I.ynga I.edulis I.macrophylla I.punctata I.alba

1 17 34 26 82

Basic classes of R objects – Character vectors

sp1 sp2 sp3 sp4 sp5

1 17 34 26 82

Page 18: R basics workshop

increm <- c(TRUE, FALSE, FALSE, TRUE, TRUE)

names(increm) <- spp

class(increm)

length(increm)

names(increm)

I.ynga I.edulis I.macrophylla I.punctata I.alba

TRUE FALSE FALSE TRUE TRUE

Basic classes of R objects – Logical vectors

Page 19: R basics workshop

Basic classes of R objects – The Matrix

Page 20: R basics workshop

• Matrices are data organized in two dimensions: rows and columns

sp1 sp2 sp3 sp4 sp5

plot_A 10 15 34 2 68plot_B 2 20 34 1 57

Columns

Rows

• Matrices can hold numeric, character or logical data

Basic classes of R objects – The Matrix

Page 21: R basics workshop

seq(1,10)

Abund <- matrix(seq(1,10), ncol=5)

abundAbund

class(abund)class(Abund)

• One way to create a matrix is the the function “matrix”

Basic classes of R objects – The Matrix

Page 22: R basics workshop

Abund.2 <- matrix(seq(1,10), ncol=5, byrow=TRUE)

AbundAbund.2

identical(Abund, Abund.2)

?matrix

• Matrices can be filled by columns (predetermined option) or by rows

Basic classes of R objects – The Matrix

Page 23: R basics workshop

v1 <- c(10,2,15,20,34,34,2,1,68,57)

Abund <- matrix(v1, ncol=5)Abund

• A matrix with abundance data:

• Vectors have length, matrices also have dimensions

dim(Abund)

ncol(Abund)nrow(Abund)

length(Abund)

Basic classes of R objects – The Matrix

Page 24: R basics workshop

spp

colnames(Abund)

colnames(Abund) <- spprownames(Abund) <- c("plot_A", "plot_B")

Abund

• For matrices, we can put names to colums and rows

Basic classes of R objects – The Matrix

• We can also put names to individual observationsnames(Abund) <-

paste("obs", 1:length(Abund), sep="")

Abund

Page 25: R basics workshop

Spp <- matrix(esp, ncol=5, nrow=2, byrow=TRUE)Spp

Increm <- matrix(increm, ncol=5, nrow=2, byrow=TRUE)

Increm

mode(Abund)mode(Spp)mode(Increm)

• Matrices can also hold character or logical values

Basic classes of R objects – The Matrix

Page 26: R basics workshop

Abundmode(abund)

sppmode(spp)

Mixed.matrix <- cbind(spp, abund)

Mixed.matrix

mode(Mixed.matrix)

• What happens when we try to merge a character and numeric vectors into the same matrix?

Basic classes of R objects – The Matrix

Page 27: R basics workshop

• Arrays are very similary to matrices… but they have 3 or more dimensions!

2003

2012

Basic classes of R objects – Array

sp1 sp2 sp3 sp4 sp5

plot_A 10 15 34 2 68plot_B 2 20 34 1 57

sp1 sp2 sp3 sp4 sp5

plot_A 12 9 15 3 75plot_B 5 27 34 0 69

Page 28: R basics workshop

sp1 sp2 sp3 sp4 Sp5

plot_A 12 9 15 3 75plot_B 5 20 34 1 69

• Arrays are very similary to matrices… but they have 3 or more dimensions!

20032012

Basic classes of R objects – Array

plot_A 10 15 34 2 68plot_B 2 20 34 1 57

Columns

Rows

3rd. dimension

Page 29: R basics workshop

Abund

Abund.array<-array(data=Abund, dim=c(2,5,2))Abund.array

class(Abund.array)

Ej.Array<-array(seq(1, 10*3), dim=c(2,5,3))Ej.Array

Basic classes of R objects – Array• Arrays are very similary to matrices… but they have 3 or

more dimensions!

Page 30: R basics workshop

• Factors contain values of a categorical variable and information ot its levels or treatments

• For example, 8 trees of I. ynga within a plot can have various fenologic states

feno.I.ynga <- c("ster.", "ster.", "flower.", "fruit.", "ster.", "flower.", "flower.", "ster.")

class(feno.I.ynga)

Feno.I.ynga <- factor(x=feno.I.ynga)

Feno.I.ynga

class(Feno.I.ynga)

Basic classes of R objects – Factors

Page 31: R basics workshop

levels(Feno.I.ynga)

length(Feno.I.ynga)

length(levels(Feno.I.ynga))

plot(feno.I.ynga)

plot(Feno.I.ynga)

• The class of an object helps determine how a function will handle it

Basic classes of R objects – Factors

Page 32: R basics workshop

• Data frames organize data in two dimensions, each column is a variable; variables can be of different types

Variables

Obs

erva

tions

spp.code Species Abund_2003 Abund_2012 Increase1 I.ynga 10 12 TRUE2 I.edulis 15 9 FALSE3 I.macrophylla 34 15 FALSE4 I.punctata 2 3 TRUE5 I.alba 68 75 TRUE

Basic classes of R objects – Data Frame

Page 33: R basics workshop

• Data frames organize data in two dimensions, each column is a variable; variables can be of different types

spp

Abundt(Abund)

increm

spp.code<-1:length(spp)

Data<-data.frame(spp.code, spp, t(Abund), increm)

Data

Basic classes of R objects – Data Frame

Page 34: R basics workshop

class(Data)

Data.M <- as.matrix(Data)

class(Data.M)Data.M

mode(Data.M)

dim(Data)dim(Data.M)

length(Data)length(Data.M)

Basic classes of R objects – Data Frame

Page 35: R basics workshop

x <- c("a", "b")y <- 1:5z <- 1:6

x.Y <- data.frame(x,y)

• Elements (columns) in a data frame must have the same length

length(x)length(y)length(z)

x.z <- data.frame(x,z)

x.z

Basic classes of R objects – Data Frame

Page 36: R basics workshop

Basic classes of R objects – R’s List

Page 37: R basics workshop

Ca Mg pH

plot_A 0.072 0.16 5.29plot_B 0.038 0.071 4.2

• Lists can contain data of differnt types, dimensions and even classes

Basic classes of R objects – List

sp1 sp2 sp3 sp4 Sp5

plot_A 12 9 15 3 75plot_B 5 20 34 1 69

20032012

plot_A 10 15 34 2 68plot_B 2 20 34 1 57

Temp. Prec.

2003 24 262012 15 19

List

1

2

3

Page 38: R basics workshop

Soils.plot <- matrix(c(0.072, 0.16, 5.29, 0.038, 0.071, 4.2), byrow=TRUE, nrow=2)

Climate.year <- matrix(c(24, 26, 15, 19), byrow=TRUE, nrow=2)

ListData <- list(Abund, Soils.plot, Climate.year)

ListData

Basic classes of R objects – List

Page 39: R basics workshop

class(ListData)

dim(ListData)

length(ListData)

names(ListData)

names(ListData)<-c("Abund.", "Soils", "Climate")

ListData

str(ListData)

Basic classes of R objects – List

Page 40: R basics workshop

v1 <- rnorm(100, 10, 5)v2 <- v1 + rnorm(100, 0, 2)

plot(v2~v1)

LM.v2v1 <- lm(v2~v1)

summary(LM.v2v1)

class(LM.v2v1)

str(LM.v2v1)

Other classes of R objects• There is a large number of other R objects,

• Most rely on the same structure as vectors, matrices, data frames and lists. E.g.:

Page 41: R basics workshop

Basic classes of R objects

Class Type of data it holds Various types of data possible?

numeric (vector) numeric No

character (vector) character No

logical (vector) logical No

matrix numeric, character or logical No

array numeric, character or logical No

factor numeric or character No

data.frame numeric, character and/or logical Yes

list numeric, character and/or logical Yes

Page 42: R basics workshop

Exercise 3Objects