R basics workshop J. Sebastián Tello Iván Jiménez Center for Conservation and Sustainable Development Missouri Botanical Garden
Jan 21, 2016
R basics workshopJ. Sebastián TelloIván JiménezCenter for Conservation and Sustainable DevelopmentMissouri 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
• 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
• 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)
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 “<-”:
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)
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
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
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
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
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
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
• 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
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
• 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
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
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
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
Basic classes of R objects – The Matrix
• 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
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
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
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
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
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
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
• 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
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
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!
• 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
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
• 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
• 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
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
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
Basic classes of R objects – R’s List
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
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
class(ListData)
dim(ListData)
length(ListData)
names(ListData)
names(ListData)<-c("Abund.", "Soils", "Climate")
ListData
str(ListData)
Basic classes of R objects – List
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.:
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
Exercise 3Objects