R course
Daniel Vaulot
2023-06-21
Introduction to R
Mid 1970s - S Language for Statistical Computing conceived by John Chambers, Rick Becker, Trevor Hastie, Allan Wilks and others at Bell Labs
Early 1990’s - R was first implemented in the early 1990’s by Robert Gentleman and Ross Ihaka, both faculty members at the University of Auckland.
1995 - Open Source Project
1997 - Managed by the R Core Group
2000 - First release of R
2011 - First release of R studio
:::
Cheat sheets
Microbes course
[1] "Hello world"
Type in script window
* Select and execute (CTRL-R)
* Source the script
Variables are abstracting your data.
Variables are objects
[1] "Hello world"
[1] 3
You can view the values of the objects in R-studio environment window (top-right)
[1] 3
Myvariable
, Myvariable1
, Myvariable.1
,Myvariable_01
are OK
1Myvariable
, My-variable
, Myvariable@
are not OK
character: “Daniel”, “This is a course in R”, ‘Joe Biden’
numeric: 2, 15.5, 10e-3
integer: 2L (the L tells R to store this is an integer)
date: 2018-02-25
logical: TRUE, FALSE
complex: 1+4i (complex numbers with real and imaginary parts)
No data “NA”
Not a number “NaN” (e.g. division by zero)
Vector
List
Matrix
Data frames
Function
\[\begin{bmatrix}10 \\ 20 \\ 30 \end{bmatrix}\]
\[\begin{bmatrix}10 \end{bmatrix}\]
[1] 10 20 30
[1] 10
Apply functions (we will come back to functions latter)
[1] "integer"
[1] 21
What is the type and length of PoTU ?
Operator | Description | |
---|---|---|
+ | addition | |
- | subtraction | |
* | multiplication | |
/ | division | |
^ or ** | exponentiation | |
x %% y | modulus (x mod y) 5%%2 is 1 | |
x %/% y | integer division 5%/%2 is 2 |
We are performing vector operations !
\[\begin{bmatrix} 1\\2\\3\\..\end{bmatrix}+\begin{bmatrix}1\\2\\3\\..\end{bmatrix}=\begin{bmatrix}2\\4\\6\\..\end{bmatrix}\]
Think about it as adding 2 columns in Excel.
[1] 3
[1] 2 4 6 8 10 12 14 16 18
Use the other operators
[1] 2 3 4 5 6 7 8 9 10
[1] 1
No error but…
The resulting variable is transformed to a numeric
How you would show that ?
Operator | Description | |
---|---|---|
< | less than | |
<= | less than or equal to | |
> | greater than | |
>= | greater than or equal to | |
== | exactly equal to | |
!= | not equal to | |
!x | Not x | |
x | y | x OR y | |
x & y | x AND y | |
isTRUE(x) | test if X is TRUE |
Do not mix
Error in first + last: argument non numérique pour un opérateur binaire
Generates an error
What can we do ?
Functions perform specific task on objects
Let’s apply paste :
[1] "Jo Biden"
Can you read the help and suggest a change in the way we call the function ?
If you write 3 times the same piece of code, then write a function…
[1] 30
Write a function to compute a product
Most of the time you do not have to write functions because someone has already written one for what you want to do…
[1] 5050
[1] -0.5113399
[1] 1.336385
What is this “library()”
Packages are set of functions that have a common goal.
They are really the strength of R
Download on your computer the package you need
Install package stringr (to manipulate strings of characters)
To use functions from the package
package::function
[1] "Jo Biden"
OR
[1] "Jo Biden"
Sometimes functions from different libraries have similar names
R - Introduction