Key lecture for the EURO-BASIN Training Workshop on Introduction to Statistical Modelling for Habitat Model Development, 26-28 Oct, AZTI-Tecnalia, Pasaia, Spain (www.euro-basin.eu)
2. 2
OUTLINE
• What is R?
• Installation
• First session in R
• Working directory
IN THIS TALK
• Getting help in R
• Editors and GUIs for R
• Installing and updating packages
• Useful packages for habitat modelling
• Documentation
• R language: type of objects, functions to manipulate them, …
• Import/export data
• Plots in R FOR BACKGROUND
• Linear models, generalised linear models (very introductory)
• Programming in R
3. 3
WHAT IS R?
• R is a language and environment for statistical computing and graphics.
• R provides a wide variety of statistical and graphical techniques, and is highly
extensible.
• R is a GNU project which is similar to the S language and environment which was
developed at Bell Laboratories by John Chambers and colleagues. R is also known
as “GNU S”.
• R is completely free and it is available as Free Software under the terms of the Free
Software Foundation GNU General Public License in source code form.
• It compiles and runs on a wide variety of UNIX platforms and similar systems,
Windows and MacOS.
• R is object-oriented.
• R is mostly command-line driven (although various graphical interfaces have been
developed).
• R has developed rapidly, and has been extended by a large collection of packages.
• Web page: www.r-project.org
4. 4
INSTALLATION
• Sources, binaries and documentation for R can be obtained via CRAN, the
“Comprehensive R Archive Network”
• For Windows:
Download the binary installer “R-2.13.2-win.exe”.
Just double-click on the icon and follow the instructions.
The default path is: “C:Program FilesRR-2.13.1”
5. 5
FIRST SESSION IN R
• Ways to open a session in R:
1. If you double-click on the icon, “Rgui” (graphical user interface) will open
2. From a system window, execute “Rterm”
3. Open R from Tinn-R, Xemacs or similar.
7. 7
THE R CONSOLE
• > indicates that R is waiting a new command
• + indicates that the previous command was uncomplete and continues reading.
• Different commands are given in different lines or in the same line separated by ;
• Comments are written adding #. Everything after this symbol is not read by R
• R distinguises capital and lower case letters. “A” is not the same as “a”.
• Type of commands:
Expressions: the command is evaluated and printed on the screen. Nothing is saved
3+2 or sum(3,2)
Assignments: the command is evaluated and saved as an object using <-
Nothing is printed. Need to type the name of the object or
use the function print() to see it.
a <- 3+2
a
print(a)
8. 8
WORKING DIRECTORY
• To know the working directory of the current session type: getwd()
• To change the working directory: setwd(whatever)
• Alternatively, execute R from the directory using a shortcut:
– Create a directory
– Right-click the R icon, go to “properties” and copy the “shortcut” path in “Start in”
• If we use Tinn-R, the default working directory is the one in which the R script is
saved
• To save the current workspace use save.image(). By default the workspace will be
called “.Rdata”. We can specify a name using
save.image(“myworkspace.Rdata”)
• To quit an R session, q()
• Be careful, in windows the paths should be given either as:
setwd("C:tmpRcourse")
setwd("C:/tmp/Rcourse")
10. 10
EDITORS
• Useful to have a text editor that allows us to keep the code scripts (with comments,
ordered, etc)
• Desirable properties for the text editor: syntax highlighting, checking parenthesis, etc,
the code can be directly sent to R without (copy-paste)
• R for Windows has a small text editor. File/Open/New script. It links directly with R
(Select code and Ctrl + R) but doesn’t allow syntax highlighting, etc.
• I use Tinn-R (only for Windows) http://sourceforge.net/projects/tinn-r
• It needs to run R in mode SDI and to install the packages R2HTML and SciViews. It
might need to change the file Rprofile.site
• Other alternatives: Emacs/ESS, Rstudio, Vim, jEdit, JGR, Eclipse,
• See a complete list in: http://sciviews.org/_rgui/projects/Editors.html
11. 11
GUIs for R
• The R command line interface (CLI) is powerful because it allows direct control on
calculations and it is flexible. However, good knowledge of the language is required.
The CLI is intimidating for beginners. The learning curve is typically longer than with a
graphical user interface (GUI), although it is recognized that the effort is profitable
and leads to better practice.
• Several projects are developping alternate user interfaces. See ongoing projects:
http://sciviews.org/_rgui/
• An example: RCmdr http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/
12. 12
PACKAGES
• All R functions and datasets are stored in packages. Only when a package is loaded
are its contents available.
• To see which packages are installed at your site, issue the command
> library()
• To load a particular package called “mgcv” use a command like
> library(mgcv)
• To see which packages are currently loaded, use
> search()
• Help for a specific library:
library(help="mgcv")
help("mgcv-package")
• Detach the loaded library:
detach("package:mgcv")
13. 13
PACKAGES
• The standard (or base) packages are considered part of the R source code. They
should be automatically available in any R installation.
• There are thousands of contributed packages for R, written by many different
authors. Some of these packages implement specialized statistical methods, others
give access to data or hardware, and others are designed to complement textbooks.
Some (the recommended packages) are distributed with every binary distribution of
R. The rest packages should be downloaded individually from CRAN.
• The packages in CRAN can be installed and updated in two ways:
– From the Rgui menu
– From the R console using the commands:
install.packages(‘R2WinBUGS’)
update.packages(‘R2WinBUGS’)
15. 15
DOCUMENTATION
• R manuals
• FAQs, Wiki,…
• Reference cards
• R News
• R Journal
• A lot of material in the web, e.g.:
The R graph Gallery: http://addictedtor.free.fr/graphiques/
R bloggers: http://www.r-bloggers.com/
16. 16
OBJECTS
• Everything (almost) in R is an object
• They are the entities that are created and saved in an R session
• They can be numbers, characters, functions, vectors, matrices, etc
• ls() or objects() show the objects created
• rm(a) removes and object called “a”.
17. 17
TYPE OF OBJECTS
• Vector: unidimensional collection of elements of the same type (numbers,
TRUE/FALSE, characters, …)
• Matrix: Bidimensional collection of elements of the same type
• Array: multidimensional collection of elements of the same type
• Data frame: like array, but allowing each column to be of different type
• Functions: code
• Factor: categorical vector
• List: a generalised vector. Each component can be of different type and can at the
same time have its own components
20. 20
LOGICAL VECTORS
• A logival value can take the value: TRUE
(T), FALSE (F) or NA (not available)
• a <- c(TRUE,FALSE,NA)
• b <- c(T,F,NA)
• rep(a,3)
20
22. 22
MISSING VALUES
• NA (not available)
x <- c(1, 2, NA, 4)
is.na(x)
sum(x); sum(x, na.rm=T)
• NaN (not a number)
0/0; Inf – Inf
3/0
x <- c(3, NA, NaN)
is.na(x); is.nan(x)
22
23. 23
CHARACTER VECTORS
• The characters are defined by “ “
• n new line, t tab, b white space
• c(“h”,”o”,”l”,”a”)
• paste(“h”,”o”,”l”,”a”)
• paste(“h”,”o”,”l”,”a”, sep=“”)
• paste(“x”,1:3, sep=“”)
• nchar(“hola”)
• substring(“hola”, 1:4, 1:4)
23
24. 24
ACCESSING PART OF VECTOR
• Namevector [index]
• x <- seq(-1,7)
• y <- x <= 5
• x[1]; x[c(1,6)]; x[1:4]; x[c(2, 5:6)]
• x[y]; x[!y]; x[x > 0]
• x[-1]; x[-(3:4)]
• x[7] <- 0; x[3:4] <- c(11,9)
• x[y] <- NA
• is.na(x)
• x[is.na(x)] <- 0
24
25. 25
FACTORS
• ?factor
• x <- c(rep(“blue”,2), “green”,rep(”red”,4))
• x
• x <- factor(x)
• x
• z <- factor(substring(“hola”,1:4,1:4),
levels=letters)
• z
• y <- factor(1:4)
• y
25
29. 29
FUNCTIONS
• Examples
ls()
a <- sum(1:6)
rm(a)
• General structure of a function:
name (arg1, arg2, arg3)
• The arguments can be given in order name (arg1, arg2, arg3) or by
name name (arg2=x2, arg3=x3, arg1=x1)
46. 46
R PROGRAMMING
• If (condition) { instructions }
• If (condition) { instructions }
else {instructions}
• while (condition) { instructions }
• For (i in index) { instructions }
46
47. 47
OWN FUNCTIONS
• Open a text editor to correct/create
functions: fix(nombre)
• Estructure:
function (arg1, arg2,arg3){
instructions
return(result)
}
47