Date post: | 30-Apr-2017 |
Category: |
Documents |
Upload: | ali-umair-khan |
View: | 213 times |
Download: | 0 times |
Ice Break of RAn Introduction to the R Programming Language
Jinlong Zhang
Kadoorie Farm & Botanic Garden
November 25, 2011
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 1 / 39
Table of Contents
1 IntroductionHistoryAdvantages & Disadvantages
2 R BasicsCRAN & PackagesObjects
3 R graphicsPlots ExamplesCreate a scatter plot step by step
4 ProgrammingScripting
5 Further Reading
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 2 / 39
What is R?
R is an open source software for statistical computing. The commandsthat driving R is also called R programming language.
The homepage of R
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 3 / 39
A Brief History
R is a dialect of S language, a statistical programming languagedeveloped by Dr. John Chambers at A&T lab in the 1970s.
S+ in an software that implementing S, and many additional features.Before the launch of R, S+ has been widely used in statistics andmodeling.
The first version of R was developed by Robert Gentleman and RossIhaka, at the University of Auckland around 1997, for teaching S+.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 4 / 39
The Characteristics of R
Open SourceThe source code of R program and the extensions could be examinedline by line.
Integrating with other Programming LanguageR is an interpreting language, can be rather slow, but could integratewith high efficient languages such as C, C++ or Fortran
OS independenceUNIX, Linux, Windows, MacOS, FreeBSD...
Command line DrivenYou have to write Commands...
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 5 / 39
Why R?
The most extensive modeling resources in scientific research
The fine publishing quality graphs
Easy to develop your own model
R is free, GNU
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 6 / 39
The Disadvantages
R is slowR is an interpreting language and is not very fast.
Could be 1/40 of C.
Limitation of MemoryAll the objects are in memory.
R is hard to learnOne has to memorize the commands/functions, and understand thelogics of programming.The fluency in R requires great time and energy.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 7 / 39
CRAN The Comprehensive R Archive Network
88 Sites in 36 RegionsThe software and packages are deposited on CRAN mirrors
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 8 / 39
First Step
R could be download athttp://www.r-project.org/at any CRAN mirror around the world.There are 87 Sites in 36 Regions, which are called ”CRAN”, and currentlyhosting R and the 3400 packages.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 9 / 39
R Graphical User Interface
The R core Team provides a simple GUI for R.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 10 / 39
Other GUIs
Rstudio, available on Linux, Windows, MacOS
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 11 / 39
R Package
A Package is a collection of R functions with comprehensive documents.A Package includes: R functions, Data Example, Help Files, Namespaceand Description.The default installation is kept as minimum.The function of R could be extent by loading R packages.
Phylogenetics
Multivariate
Bayesian statistics
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 12 / 39
The Packages I
ade4 Analysis of Ecological Data using Euclidean methodsape Analysis of Ecology and Evolutionboot Bootstrapcluster Cluster Analysisecodist Ecological Distancee1071 Misc Functions of the Dept. Stat. TU WienMASS Venables and Ripley’s MASSlattice Lattice Graphics
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 13 / 39
The Packages II
maptools Tools for reading and handling spatial objectsmvpart Multivariate partitioningnlme Linear and Nonlinear Mixed Effects Modelsouch Ornstein-Uhlenbeck modelsraster Geographic analysis and modeling with raster datasp classes and methods for spatial dataspatstat Spatial Point Pattern analysis, model-fittingvegan Community Ecology Package
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 14 / 39
CRAN Task View
The packages are reviewed periodically by profounding experts, the resultsare appeared online as CRAN TaskViews.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 15 / 39
Homepage of Package Vegan
Description and the checking results of packages could be found at CRANMirror.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 16 / 39
Install Packages
To install a package, just type
R code
install.packages(”vegan”)
Or,Packages > install packages from local files
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 17 / 39
Use a Package
Packages must be loaded into memory before the functions could be called.
R code
library(”vegan”)
There are comprehensive documentations for R functions, to look at thehelp files, just type, for example:
R code?vegan
help(”vegan”)help.search(”t.test”)
or RGui>Help>Html help
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 18 / 39
Html of Help
Help page
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 19 / 39
Contents of Help Files
lm{stats} function and the package nameFitting Linear Models Title of the functionDescription The descriptionUsage Parameters of the functionArguments Parameters in detailDetails How the algorithms implementedAuthor(s) Author of the functionReferences Literature CitedExamples Example of the function
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 20 / 39
R as a calculator
You can directly input R commands:
R code2 + 2
a <− 2
Assignment<−, =, or even −>
R codeb <− 2
c <− a+b
c
# Comments
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 21 / 39
Objects in R
Type of elementsNumeric 100, 0, -4.335Character ”China”Logical TRUE, FALSEFactor Different levelsComplex 2 + 3i
Type of ObjectsVector c(1,2,3); c(”a”,”a”,”b”,”b”,”c”)Matrix x <− 1:12 ; dim(x) <− c(3,4)Dataframe Combined vectors of different typesList Container of Different objectsArray Matrix with different dimensions
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 22 / 39
Operators
Math
+, −, ∗, /, ∧
Comparisons
>, <, <=, >=, ==, !=
Logical
!, &, &&, |, ||
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 23 / 39
Creating Vectors
Character
character<−c("China", "Korea", "Japan", "UK", "USA",
"France", "India", "Russia")
Numerical
numeric<−c(1, 3, 6, 7, 3, 8, 6, 4)
Logical
logical<−c(T, F, T, F, T, F, F, T)
Function to Creat Vectors
c(2,5,6,9)
rep(2,times=4)
seq(from=3, to=21, by=3 )
1:15
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 24 / 39
Graphics generated by R
The R basic graphs
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 25 / 39
Graphics generated by R
Ancestral Reconstruction using R
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 26 / 39
Graphics generated by R
Handling: SHP files, Converstion between Projections, Raster data,extract values, Krigging, Spatial Autoregressive Models
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 27 / 39
Graphics generated by R
Krigging and spatial analysis
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 28 / 39
Graphics generated by R
Results of spatial analysis
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 29 / 39
Higher Plotting Functions
plot() Scatter plothist() Histogramsboxplot() Boxplotstripchart() stripchartbarplot() barplotpiechart() Pieslegend() Add legends
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 30 / 39
Lower Plotting Functions
lines() add line to plotcurve() add curveabline() add straight linepoints() add pointssegments() Add segmentsaxis() add axisbox() Add box to the plottitle() Add titletext() Add text to plot
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 31 / 39
Creating a scatter plot I
Original Data
x <- runif(50,0,2)
y <- runif(50,0,2)
Step 1
plot(x, y, type=”n”, xlab=””, ylab=””, axes=F)
Step 2
points(x,y)
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 32 / 39
Creating a scatter plot II
Step 3
axis(1);
axis(at=seq(0.2,1.8,0.2), side=2)
Step 4
box()
Step 5
title(main=”Main title”, sub=”subtitle”, xlab=”x-label”,ylab=”y-label”)
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 33 / 39
Creating a scatter plot III
A plot step by step
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 34 / 39
Scripting
Save the R code in a plaint text editor, with an extension .rThe code could be implemented later.
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 35 / 39
Code Highlighting
Highlighting of key words
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 36 / 39
Text Editors: TinnR
Code Highlighting by TinnR
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 37 / 39
Further Reading
Selected Publications
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 38 / 39
Many Thanks!Questions?
Jinlong Zhang ( Kadoorie Farm & Botanic Garden ) Ice Break of R November 25, 2011 39 / 39