-
Laurent Modolo authoredLaurent Modolo authored
- R#1: Introduction to R and RStudio
- Functions are also variables
- Functions are also variables
- Functions are also variables
- Functions are also variables
- The environment
- A code editor
- A code editor
- A code editor
- Installing packages
- Installing packages
- Loading packages
- Complex variable type
- Vector (aka list)
- Vector (aka list)
- Vector challenge
- Vector challenge
- Vector challenge
- Vector challenge
- Vector challenge
- Vector challenge
- Matrix
- DataFrame
title: 'R#1: Introduction to R and RStudio'
author: "Laurent Modolo [laurent.modolo@ens-lyon.fr](mailto:laurent.modolo@ens-lyon.fr)"
date: "10 Oct 2019"
output:
beamer_presentation:
theme: metropolis
slide_level: 3
fig_caption: no
df_print: tibble
highlight: tango
latex_engine: xelatex
slidy_presentation:
highlight: tango
R#1: Introduction to R and RStudio
The goal of this practical is to familiarize yourself with R and the RStudio environment.
The objectives of this session will be to:
- Understand the purpose of each pane in RStudio
- Do basic computation with R
- Define variables and assign data to variables
- Manage a workspace in R
- Call functions
- Manage packages
http://perso.ens-lyon.fr/laurent.modolo/R/session_1_a
Press [alt] + [shift] + k
Functions are also variables
We can also define our own function with
<FUNCTION_NAME> <- function(a, b){
<RESULT_1> <- <OPERATION_1>(a, b)
<RESULT_2> <- <OPERATION_2>(<RESULT_1>, b)
return(<RESULT_2>)
}
\pause
write a function to test the base of the logarithm function
\pause
base_test <- function(x, base){
log_result <- logarithm(x, base=base)
exp_result <- base^log_result
test_result <- x == exp_result
return(test_result)
}
http://perso.ens-lyon.fr/laurent.modolo/R/1_b
Functions are also variables
base_test <- function(x, base){
print(x)
log_result <- logarithm(x, base=base)
print(log_result)
exp_result <- base^log_result
print(exp_result)
print(x)
test_result <- x == exp_result
return(test_result)
}
http://perso.ens-lyon.fr/laurent.modolo/R/1_c
Functions are also variables
base_test <- function(x, base){
print(x)
log_result <- logarithm(x, base=base)
print(log_result)
exp_result <- base^log_result
print(exp_result)
print(x)
test_result <- isTRUE(all.equal(x, exp_result))
return(test_result)
}
http://perso.ens-lyon.fr/laurent.modolo/R/1_d
Functions are also variables
base_test <- function(x, base){
return(isTRUE(all.equal(x, base^logarithm(x, base=base))))
}
http://perso.ens-lyon.fr/laurent.modolo/R/1_e
The environment
\includegraphics[width=\textwidth]{img/RStudio_environment.png}
A code editor
\includegraphics[width=\textwidth]{img/RStudio_editor.png}
A code editor
RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can
- click on the Run button above the editor panel, or
- select “Run Lines” from the “Code” menu, or
- hit Ctrl+Return in Windows or Linux or Cmd+Return on OS X. To run a block of code, select it and then Run.
If you have modified a line of code within a block of code you have just run, there is no need to reselect the section and Run, you can use the next button along, Rerun the previous region. This will run the previous code block including the modifications you have made.
A code editor
Copy your logarithm
and base_test
into a tp_1.R
file
\pause
We can now clean your environment
rm(x)
\pause
?rm
\pause
ls()
\pause
rm(list = ls())
Installing packages
install.packages("tidyverse")
install.packages("ggplot2")
Installing packages
\includegraphics[width=\textwidth]{img/RStudio_outputs.png}
Loading packages
sessionInfo()
\pause
library(tidyverse)
\pause
sessionInfo()
\pause
unloadNamespace("tidyverse")
\pause
sessionInfo()
Complex variable type
Vector (aka list)
c(1, 2, 3, 4, 5)
\pause
1:5
\pause
2^(1:5)
\pause
x <- 1:5
2^x
\pause
log(x)
logarithm(x)
base_test(x, base = 10)
Vector (aka list)
typeof(x)
\pause
typeof(x + 0.5)
\pause
is.vector(x)
\pause
length(x)
\pause
x[5]
\pause
y <- c(a = 1, b = 2, c = 3, d = 4, e = 5)
typeof(y)
is.vector(y)
names(y)
y[1]
y["a"]
names(y) <- c("b")
\pause
x == y
all.equal(x, y)
Vector challenge
- use the
seq()
function to create a vector of even numbers - You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. - Check the default vectors
letters
andLETTERS
, rewrite your previous command using them. - Create a vector giving you the correspondence between small case letters and upper case letters.
Vector challenge
- use the
seq()
function to create a vector of even numbers
seq(from=2, to=10, by=2)
- You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. What is the type of this vector. - Check the default vectors
letters
andLETTERS
, rewrite your previous command using them. - Create a vector giving you the correspondence between small case letters and upper case letters.
Vector challenge
- use the
seq()
function to create a vector of even numbers
seq(from=2, to=10, by=2)
- You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. What is the type of this vector. - Check the default vectors
letters
andLETTERS
, rewrite your previous command using them. - Create a vector giving you the correspondence between small case letters and upper case letters.
Vector challenge
- use the
seq()
function to create a vector of even numbers - You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. What is the type of this vector.
c(1:5, "a", "b", "c")
typeof(c(1:5, "a", "b", "c"))
- Check the default vectors
letters
andLETTERS
, rewrite your previous command using them. - Create a vector giving you the correspondence between small case letters and upper case letters.
Vector challenge
- use the
seq()
function to create a vector of even numbers - You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. What is the type of this vector. - Check the default vectors
letters
andLETTERS
, rewrite your previous command using them.
c(1:5, letters[1:3])
- Create a vector giving you the correspondence between small case letters and upper case letters.
Vector challenge
- use the
seq()
function to create a vector of even numbers - You can concatenate vector with
c(<VECTOR_1>, <VECTOR_2>)
, concatenate a vector of integer with a vector of the first 5 letter of the alphabet. What is the type of this vector. - Check the default vectors
letters
andLETTERS
, rewrite your previous command using them. - Create a vector giving you the correspondence between small case letters and upper case letters.
rosette <- LETTERS
names(rosette) <- letters
rosette["b"]
rosette[13]
Matrix
In R matrix are two dimensional vectors
matrix_example <- matrix(1:(6*3), ncol=6, nrow=3)
matrix_example
\pause
class(matrix_example)
nrow(matrix_example)
ncol(matrix_example)
\pause
matrix_example[2, 3]
DataFrame
In R data.frame
are table type with mixed type
data_frame_example <- data.frame(numbers=1:26, letters=letters, LETTERS=LETTERS)
data_frame_example
\pause
class(data_frame_example)
nrow(data_frame_example)
ncol(data_frame_example)
names(data_frame_example)
\pause
data_frame_example[2, 3]
data_frame_example["numbers"]