Mails :
[Link]@[Link] ;
[Link]@[Link]
Document version: 14/09/12
A freely available language and environment
› Statistical computing
› Graphics
Supplementary packages
› Bioinformatics
› Data mining
› Database management, etc.
Compatible with other programming langages
Leslie REGAD and Gaëlle LELANDAIS
[Link]
Leslie REGAD and Gaëlle LELANDAIS
Type « R » (in a Linux terminal) or run the program (Wind. or Mac)
A new command line starts with the symbol « > »
Example :
> 1+1/4 #[press enter]
[1] 1.25
> (1+1)/4
[1] 0.5
> 21.3*2
[1] 42.6
To quit the session:
> q()
Save workspace image? [y/n/c]
# [type « n » for « no »]
Leslie REGAD and Gaëlle LELANDAIS
Performing mathematical calculations
› sin(), cos(), tan(), etc.
Working with numbers
› round(), floor(), ceiling(), etc.
Working with characters (strings)
› nchar()
› toupper(), tolower(), etc.
Performing statistical analyses
› mean(), median(), var(), sd(), summary(), etc.
› sample(), rnorm(), etc.
Leslie REGAD and Gaëlle LELANDAIS
Ask for help
› ? FunctionName ; help(FunctionName); [Link]();
> ?mean ; help(mean) ; [Link](« mean »)
Help page is divided into different sections
› Description What is the purpose of the function?
› Usage How to use the function?
› Arguments What are the parameters of the function?
› Details Technical information
› Value What are the results returned by the
function?
› See also Are there similar functions?
› Example Short tutorial of the function
Leslie REGAD and Gaëlle LELANDAIS
Choose a text editor
› Linux: gedit, kate or emacs, etc.
› Windows: Notepad++, TinnR
› Mac: gedit, R editor
Copy the command lines written in the text editor and paste
them in a R terminal
› Don’t forget to save your text file
Leslie REGAD and Gaëlle LELANDAIS
To execute R scripts (command lines are successively run)
› source(« ScriptR.R »)
Lines can be ignored (to insert comments)
› Symbole « # » at the beginning of lines
Text file (named « ScriptR.R ») : R software :
# Example of a R script
print(”my first script")
> source("ScriptR.R")
[1] ”my first script"
# Addition of two numbers
[1] 14
a = 10 + 4
print(a)
Leslie REGAD and Gaëlle LELANDAIS
Practical session #1
(R installation and command line backup)
Leslie REGAD and Gaëlle LELANDAIS
Variable assignment
> a = 1 # new R versions
> a <- 1 # old R versions
Reading / Use
> a # call of the variable
[1] 1 # the content of the variable is displayed
> a + 1 # change of the variable “on the flight”
[1] 2
Note
› An index is printed between characters « [ ] »
Leslie REGAD and Gaëlle LELANDAIS
Different types of variables
› Integers, floats
› Strings
Recommendations regarding the names of variables
› Case sensitive (uppercases or lowercases)
› Must not begin with a number or a special character
(!,;_*)
› Several names are already used by the R software (q, T, F,
D, I, var, mean, for instance)
Combination of variables into objects
› Vectors
› Tables (matrices, data frames)
› Lists, etc.
Leslie REGAD and Gaëlle LELANDAIS
Definition
› Succession of elements (or variables) that have the same
type (integers or floats, strings, etc.)
> Vect = c(1, 4, 5, 6, 57)
> Vect
[1] 1 4 5 6 57
> Vect2 = c('a', 'k', 'm', 'p')
> Vect2
[1] 'a' 'k' 'm' 'p'
Special values
› NA : Missing value (Not Available)
› NaN : Not a Number
› -Inf/Inf : Infinite symbol(+ or –)
Leslie REGAD and Gaëlle LELANDAIS
Functions to create a vector
› c(), x:y, seq(), rep(), append(), etc.
Functions to manipulate a vector
› [Link]()
› length()
› sort(), etc.
> c(1,2,3) > [Link](c(1,2,3))
[1] 1 2 3 [1] "numeric"
> 1:3 > [Link](c("A","B","C"))
[1] 1 2 3 [1] "character"
> seq(1,3) > length(c(1,2,3))
[1] 1 2 3 [1] 3
> rep(1,3) > sort(c(4,5,2))
[1] 1 1 1 [1] 2 4 5
Leslie REGAD and Gaëlle LELANDAIS
Definition
› Set of elements stored in a two dimensional table
Columns
Cell
Rows
Matrix
› Unique type for the elements
Data frame
› Columns can stored elements with different types
(integers or floats, strings, etc.)
Leslie REGAD and Gaëlle LELANDAIS
Functions to create a table
› matrix(), [Link](), cbind(), rbind(), etc.
Functions to manipulate a table
› [Link]()
› dim()
› etc.
> cbind(c(1,2),c(3,4))
[,1] [,2]
[1,] 1 3
> matrix(0,nrow=2,ncol=2)
[,1] [,2] [2,] 2 4
[1,] 0 0 > rbind(c(1,2),c(3,4))
[,1] [,2]
[2,] 0 0
[1,] 1 2
> dim(matrix(0,nrow=2,ncol=2))
[2,] 3 4
[1] 2 2
Leslie REGAD and Gaëlle LELANDAIS
Aim
› Import in R a dataset written in a text file
Available functions
Field separator
› scan() character
› [Link](), [Link]()
› readLines()
Col 1 Col 2 … Col n
Ligne 1 Val 1 Val 2 … Val n
Ligne 2 Val 1 Val 2 Val n
Ligne n Val 1 Val 2 Val n
Leslie REGAD and Gaëlle LELANDAIS
Name of the file to read
« [Link] »
Columns are
separated with a
« tab »
> [Link]("[Link]")
Col1 Col2 Col3
Line1 2 8 5
Line2 1 6 3
Leslie REGAD and Gaëlle LELANDAIS
Aim
› Export a set of results obtained in R in a text file
› This file will be opened with other softwares (Excel or
OpenOffice for instance)
Field separator
character
Available functions
› cat() Col 1 Col 2 … Col n
› write()
› [Link]() Ligne 1 Val 1 Val 2 … Val n
Ligne 2 Val 1 Val 2 Val n
Ligne n Val 1 Val 2 Val n
Leslie REGAD and Gaëlle LELANDAIS
Example
› Create a vector with 10 random numbers (according to a
normal distribution), and save the results into a text file
> data = rnorm(10)
> [Link](data, file = "[Link]")
> [Link](data, file = "[Link]", [Link] =
F, [Link] = F)
Leslie REGAD and Gaëlle LELANDAIS
Practical session #2
› Exercises 4, 5, 6
› Exercise 41
Discussion: Your needs with R?
Leslie REGAD and Gaëlle LELANDAIS