R#1: Introduction to R and RStudio
Laurent Modolo laurent.modolo@ens-lyon.fr
10 Oct 2019
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>)
}
write a function to test the base of the logarithm function
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
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
We can now clean your environment
Installing packages
install.packages("tidyverse")
install.packages("ggplot2")
Loading packages
unloadNamespace("tidyverse")
Complex variable type
Vector (aka list)
log(x)
logarithm(x)
base_test(x, base = 10)
Vector (aka list)
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")
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 integers with a vector of the first 5 letters of the alphabet.
- Check the default vectors
letters
and LETTERS
, 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 integers with a vector of the first 5 letters of the alphabet. What is the type of this vector.
- Check the default vectors
letters
and LETTERS
, 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 integers with a vector of the first 5 letters of the alphabet. What is the type of this vector.
- Check the default vectors
letters
and LETTERS
, 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 integers with a vector of the first 5 letters 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
and LETTERS
, 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 integers with a vector of the first 5 letters of the alphabet. What is the type of this vector.
- Check the default vectors
letters
and LETTERS
, 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 integers with a vector of the first 5 letters of the alphabet. What is the type of this vector.
- Check the default vectors
letters
and LETTERS
, 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
class(matrix_example)
nrow(matrix_example)
ncol(matrix_example)
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
class(data_frame_example)
nrow(data_frame_example)
ncol(data_frame_example)
names(data_frame_example)
data_frame_example[2, 3]
data_frame_example["numbers"]
List
In R list
are multitypes object
list(a = "test", b = 1:5)
z <- list(
a = "test",
b = 1:5,
c = data.frame(numbers=1:26, letters=letters, LETTERS=LETTERS)
)
list(
a = "test",
b = 1:5,
c = z
)