Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
A Brief Intro to R
A. Michelle Lawing Ecosystem Science and Management Texas A&M University College Sta,on, TX 77843 [email protected]
Adapted from Gene Hunt Session 1 -‐ hYp://paleobiology.si.edu/staff/individuals/hunt.cfm
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Background on R
• Descended from S (Bell Labs); both S and R are used heavily by sta,s,cians
• Open source, currently maintained by a volunteer commiYee
• Prac,cal – Free, available for all major OS
• Scien,fic Benefits – High level (powerful func,ons built-‐in) – Powerful sta,s,cal, graphics capabili,es – Extendable (user contributed modules)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Gegng Help in R
• Google!
• hYp://www.r-‐project.org
• Email list EEB-‐R-‐[email protected]
• help(fun) • ?fun • ??fun
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Ways to use R
• As a sta,s,cs package (ANOVA, nonparametric sta,s,cs, ordina,ons)
• For publica,on graphics
• As a programming language (resampling, morphometrics, likelihood approaches)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
The Console
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
The interac,ve prompt
> 2 2 > 2+2 4 Savings informa,on as variables > x <-‐ 8 #assigns variable x a value of 8 Assignment Operator > x * 5 40 > y <-‐ “tooth”
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Variable assignment
1. Can also use = as assignment operator y <-‐ 18 y = 18 Usually interchangeable
2. All names in R are case-‐sensi,ve tri <-‐ 18 Tri <-‐ 25 # these are different variables
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Types of variables
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Exercise in R
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Func,ons • Func,ons take informa,on (=arguments), do something
with it, and return a result sqrt() computes the square root of its argument sqrt(25) 5
seq() generates regular sequences of numbers seq(1,5) 1 2 3 4 5 1:5 1 2 3 4 5 # special shortcut
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Combining elements into arrays • Arrays are regular arrangements of mul,ple elements • Must be the same type (e.g., all numbers) • Vectors are 1D arrays, matrices are 2D arrays. Making vectors
x <-‐ c(10, 14, 35, 50) # concatenate func,on
length(x) 4 # number of elements in x
1st 2nd 3rd 4th elements of x
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Extrac,ng data
• Extrac,ng/subsegng element(s) from a vector using [ ] x[2] 14 x[2:3] 14 35
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
2D Arrays: Matrices
X <-‐ matrix(1:6, nrow=3)
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Opera,ons on arrays
• Many opera,ons and func,ons can be applied to numbers, vectors and matrices.
• Opera,ons are usually done element-‐wise x <-‐ 1:4 1 2 3 4 x+5 6 7 8 9 sqrt(x) 1 1.41 1.73 2
ESSM 689 Quan,ta,ve Methods in Ecology, Evolu,on and Biogeography
Ecosystem Science and Management | Texas A&M University (c) 2015, A. Michelle Lawing
Exercise in R