Cours R
Cours R
Objectives 4
I - Chapter 1: Introduction to the R Language 5
1. Getting started with R software ................................................................................5
2. Overview of the workspace: commands and packages in R ...................................7
3. Integrated Development Environment (IDE)............................................................8
4. Exploring the R-Studio IDE ......................................................................................10
II - Chapter 2 - Objects, Vectors, and Functions 12
1. The R code................................................................................................................12
1.1. R command .....................................................................................................................................12
1.2. comments in R.................................................................................................................................12
1.3. Objects .............................................................................................................................................12
1.4. Mathematical and comparaison opertators ..................................................................................13
1.5. some notions ...................................................................................................................................13
1.6. data types in R programming .........................................................................................................14
1.7. Vectors .............................................................................................................................................15
1.8. Lists ..................................................................................................................................................20
2. Functions in R ..........................................................................................................20
2.1. Type of Functions in R .....................................................................................................................20
2.2. help on R for Built-in Functions ......................................................................................................21
2.3. a user-defined function...................................................................................................................21
2
Table of contents
3
Objectives
R is both statistical software and a programming language used for manipulating data, creating
visualizations, and conducting statistical analyses on that data. For several years, R has established itself
as the reference for data analysis and graph production in a wide range of industries. The goal of this
module is to explore the R software step by step, enabling participants to acquire the knowledge and
skills needed to work effectively in this environment.
4
Chapter 1: Introduction to the R
Language I
Definition
Example on Windows :
5
Chapter 1: Introduction to the R Language
Download options
CRAN mirror
A CRAN mirror is a global server hosting a copy of the Comprehensive R Archive Network (CRAN),
enabling users to download R software, packages, and resources. Choosing a closer CRAN mirror
enhances download speed during R installation or updates.
R Mirrors
Selecting a Mirror
6
Chapter 1: Introduction to the R Language
Starting R
Starting on Windows : When you start R, it opens a new window. The window includes a text pane,
called the R Console, where you enter R expressions
check the
getwd() current working
directory in R
set working
setwd()
directory
7
Chapter 1: Introduction to the R Language
In contrast, “max(1,3,” is an incomplete expression, so R prompts you for more input. The prompt
changes from greater-than (>) to plus (+), letting you know that R expects more:
RStudio IDE
RStudio is an Integrated Development Environment (IDE) designed for the R programming language. It
offers a user-friendly interface with features such as a script (code) editor, console, data viewer, and
integrated tools for R package management, plotting, and project organization. RStudio is widely used
for R programming and data analysis tasks.
8
Chapter 1: Introduction to the R Language
9
Chapter 1: Introduction to the R Language
Graphic 4
Key characteristics of R scripts include:
Plain Text Format: R scripts are plain text files, often created using a text editor or an Integrated
Development Environment (IDE) like RStudio. This makes them human-readable and easy to
share.
Code Sequences: R scripts consist of a sequence of R commands, statements, and expressions.
These can include data manipulations, statistical analyses, plotting, and more.
Reproducibility: One of the main advantages of using R scripts is the ability to achieve
reproducibility. By saving your R code in a script, others (or your future self) can rerun the script
to reproduce the same results.
Documentation: R scripts can include comments (lines preceded by the # symbol) to provide
explanations and documentation for the code. This helps make the code more understandable
for others and for future reference.
Functions and Control Structures: R scripts can define functions and use control structures
(e.g., loops and conditional statements) to create more complex and organized code.
Example :
1 x <- 10 # creates a numeric object 'x' with the value 10
2 vector <- c(1, 2, 3) # creates a numeric vector 'vector'
10
Chapter 1: Introduction to the R Language
The console
the console serves as an interactive interface for executing R commands and receiving immediate
feedback. Here are some key aspects of the console in RStudio:
Immediate Output: When you enter a command in the console and press Enter, R immediately
processes the command and displays the output or result right below the command
History:The console keeps a history of previously executed commands.
Variable Inspection : After executing commands that create variables or objects, the console displays
information about these objects, such as their values, types, and dimensions.
Error Messages and Warnings : If there are errors or warnings in your code, the console will display
relevant messages, helping you identify and address issues in your R script.
Interactive Help :You can access interactive help by typing ? followed by the function or topic you
want help with. The console will display relevant help documentation.
11
Chapter 2 - Objects, Vectors, and
Functions II
1. The R code
1.1. R command
A command refers to a line of code or a statement that instructs the R interpreter to perform a specific
action or computation. R commands can be simple, like assigning a value to a variable, or they can be
more complex, involving functions, data manipulation, statistical analysis, and more.
Example : (Variable Assignment)
1 x <- 10
1.2. comments in R
In R programming, comments are annotations within the code that are not executed as part of the
program. They are used to provide explanations, clarify code, or add notes for the programmer.
Comments in R are preceded by the hash symbol (#). Anything following the # on a line is treated as a
comment and is ignored by the R interpreter.
1 print("Hello, R!") # Displays the text "Hello, R!" in the console
1.3. Objects
In R programming, an object is often used interchangeably with the term "variable." In R, variables are
used to store and manipulate data. When you create a variable and assign a value to it, you are
essentially creating an object.
The rm() function is used to remove (delete) one or more objects from the R workspace. It helps in
freeing up memory by removing unnecessary objects.
Example:
1 rm(my_variable) # Remove the object 'my_variable' from the workspace
12
Chapter 2 - Objects, Vectors, and Functions
To remove all objects in the workspace, you can use rm(list = ls()):
1 rm(list = ls()) # Remove all objects in the workspace
Comparison operators in R
13
Chapter 2 - Objects, Vectors, and Functions
2. Using reserved words or system names such as break, else, for, function, if, in, next, repeat, return,
while, TRUE, FALSE, Inf, NA, NaN, NULL, NA_integer_, NA_real_, NA_complex_, NA_character_, ..., ..1,
..2, etc.
1 if<-10
2 Error: unexpected assignment in "if<-"
Using special characters other than period "." and underscore "_".
1 *x<-20
2 Error: unexpected '*' in "*"
The variables T and F default to the values TRUE and FALSE, respectively, but can be reassigned
1> T
2 [1] TRUE
3> F
4 [1] FALSE
reassigning :
> T<-10
>T
[1] 10
> F<-20
>F
[1] 20
"hello", 'R
Character Textual data enclosed in quotes
programming'
Complex Numbers with real and imaginary parts 3 + 4i, -2.5 - 1.8i
"High", "Medium",
Factor Categorical data with fixed levels
"Low"
"2024-02-12",
Date & Time Dates, times, or date-time objects
"09:30:00"
14
Chapter 2 - Objects, Vectors, and Functions
In R, you can use the following functions to determine the type of an object:
typeof(): This function returns the type of an object as a string
Example :
1 > x<-10.2
2 > z<-TRUE
3 > typeof(x)
4 [1] "double"
5 > typeof(z)
6 [1] "logical"
1.7. Vectors
a) Vectors Creation and related functions and operations
Using the c() function:
You can create a vector by combining elements using the c() function.
Example :
1 > v <- c(1, 2, 3, 4, 5)
2 > print(v)
3 [1] 1 2 3 4 5
15
Chapter 2 - Objects, Vectors, and Functions
label-value vector
To add labels from one vector and values from another vector in R, you can use the names() function
to assign labels to the elements of the first vector
1 l <- c("A", "B", "C") # Vector of labels
2 v <- c(10, 20, 30) # Vector of values
3
4 # Assign labels to the elements of the values vector
5 names(v) <- l
6v
you can add direcltly label when defining the vectos as follow :
1 v <- c(A=10, b=20, c=30) # Vector of labels and values
2v
Vector indexing
The indexing in a vector is done using square brackets [ ]. One can extract an element from a vector by
its position or by its label, if it exists (in which case this approach is much safer).
Example (position index):
1 v <- c(A=10, b=20, c=30) # Vector of labels and values
2 v[1] #extract the value at position 1
16
Chapter 2 - Objects, Vectors, and Functions
You can extract a part or subset from a vector by specifying the start and end indexes within square
brackets.
Example :
1 v <- c(A=10, b=20, c=30) # Vector of labels and values
2 x<-v[1:2] #extract the part from index 01 to 2
3 print(x)
4
5A b
6 10 20
17
Chapter 2 - Objects, Vectors, and Functions
Example:
1 mon_vecteur = c(3.14, "A")
2 print(mon_vecteur)
3
4 [1] "3.14" "A"
example 02
1 x<-3.14
2 y<-"A"
3 mon_vecteur = c(x,y)
4 print(mon_vecteur)
5 [1] "3.14" "A"
6
7 typeof(x)
8 [1] "double"
9 typeof(y)
10 [1] "character"
11
12 typeof(mon_vecteur[1])
13 [1] "character"
14 typeof(mon_vecteur[2])
15 [1] "character"
18
Chapter 2 - Objects, Vectors, and Functions
The rearrangement
The rearrangement of a vector refers to changing the order of its elements. This can involve sorting the
elements in ascending or descending order, shuffling them randomly, or arranging them based on
specific criteria.
Example:
1 my_vector <- c(3, 1, 4, 1, 5, 9, 2, 6)
2
3 sorted_vector <- sort(my_vector) # Sort the vector in
ascending order
4 print(sorted_vector)
5 [1] 1 1 2 3 4 5 6 9
6
7 sorted_desc_vector <- sort(my_vector, decreasing = TRUE) #Sort the vector in
descending order
8 print(sorted_desc_vector)
9 [1] 9 6 5 4 3 2 1 1
10
11 shuffled_vector <- sample(my_vector, replace = FALSE) # Shuffle the vector
randomly
12 print(shuffled_vector)
13 [1] 1 5 2 4 6 1 3 9
14
15 # Rearrange the vector based on specific criteria # For example, arranging even
numbers before odd numbers
16 sorted_evens_odds <- c(sort(my_vector[my_vector %% 2 == 0]),
sort(my_vector[my_vector %% 2 != 0]))
17 print(sorted_evens_odds)
18 [1] 2 4 6 1 1 3 5 9
19
19
Chapter 2 - Objects, Vectors, and Functions
1.8. Lists
a) Interest of lists
Lists are a highly flexible and widely used data structure in .
A list is a vector whose elements are not necessarily of the same type.
An element of a list is any object, including another list.
The list() function is used to create lists.
Elements of the list are typically accessed by their names (using the $ operator).
1 # Create a list with different types of elements
2 my_list <- list(name = "John", age = 30, scores = c(90, 85, 95), married = TRUE)
3
4 my_list[[1]] # Accesses the first element of the list
5
6 my_list$name # Accesses the element named "name"
7
8 my_list$level<-"Master" # Add an element to a list
9
10 my_list$level=NULL #delete an elemnt by assigning Null value
11
12 my_list <- my_list[-4] #remove the 4 th elemnt
2. Functions in R
2.1. Type of Functions in R
Built-in Functions:
hese are functions that are pre-defined in the R language and are available for immediate use without
the need to load any additional packages. They are part of the base R system and provide basic
functionality for data manipulation, mathematical operations, statistical analysis, etc.
20
Chapter 2 - Objects, Vectors, and Functions
User-defined Functions
These are functions created by users to perform specific tasks that are not already covered by built-in
functions. Users can define their own functions using the function() keyword, specifying the
desired behavior and tasks to be performed by the function.
21
Exercice series III
1. Quiz: N°(1)
Question [solution n°1 p. 35]
2. Quiz: N°(2)
Question [solution n°2 p. 35]
1. Create the vector "vec1" containing the sequence of integers from 1 to 12.
2. Append the values 16, 17, 18 to the end of this vector.
Hint:
":", c()
3. Quiz: N°(3)
Question [solution n°3 p. 35]
4. Quiz: N°(4)
Question [solution n°4 p. 36]
1. Create the vector "vec3" containing all multiples of 2 between 2 and 52.
2. Create the vector "vec4" containing the letter A once, the letter B twice, and the letter C three times.
3. What is the length of this sequence?
Hint:
seq(), rep(), c(), length()
22
Exercice series
5. Quiz: N°(5)
Question [solution n°5 p. 36]
Hint:
paste(), ChatGPT, help()
6. Quiz: N°(6)
Question [solution n°6 p. 36]
Let:
1 SPE = c("GSI", "ACG", "E-BUSINESS", "DFBM", "MD")
7. Quiz: N°(7)
Question [solution n°7 p. 37]
Create, (in a single command line), the vector VE containing the following names:
1 chr1, chr2, ..., chr22, chrX, chrY.
Hint:
paste()
8. Quiz: N°(8)
Question [solution n°8 p. 37]
Let:
1 NOTES = c(15, 20, 5, NA, 12, 10, 8, 16, 18, 6, 3, 20, NA, 14, 9)
23
Chapter 3- Mastering R Studio:
Options, Importing, Exporting, and
Packages IV
24
Chapter 3- Mastering R Studio: Options, Importing, Exporting, and Packages
25
Chapter 3- Mastering R Studio: Options, Importing, Exporting, and Packages
Graphic 10 step 01
Graphic 11 Step 02
Graphic 12 Step 03
26
Chapter 3- Mastering R Studio: Options, Importing, Exporting, and Packages
27
Chapter 4: Data Frames (data.frame) V
1. Creating a data.frame
Data Frames are tabular data structures in R.
They can contain a mix of different data types, but each column must have consistent data types.
Create a data frame using the data.frame() function. dd data frame result.png (cf. p.28)
1 # Create a data frame
2 Data_Frame <- data.frame (
3 Name = c("Amin", "Omar", "Aya"),
4 Average = c(10, 15, 12),
5 R_note = c(7, 14, 12)
6)
7
8 # Print the data frame
9 Data_Frame
28
Chapter 4: Data Frames (data.frame)
29
Chapter 4: Data Frames (data.frame)
3. Access Items
you can access items using different ways like :
1 Data_Frame[1]
2 Data_Frame[["Average"]]
3 Data_Frame$R_note
4. Add Rows
Use the rbind() function to add new rows in a Data Frame:
Example :
1 # Add a new row
2 New_row_DF <- rbind(Data_Frame, c("Ahmed", 110, 110))
3
4 # Print the new row
5 New_row_DF
30
Chapter 4: Data Frames (data.frame)
5. Add Columns
Use the cbind() function to add new columns in a Data Frame:
31
Chapter 4: Data Frames (data.frame)
7. Access an element
To access an element in a data frame you can use row and column indices or column names
example
1 # Access element in the third row and "R_note" column
2 element <- Data_Frame[2, "R_note"]
32
Chapter 4: Data Frames (data.frame)
8. Transform variables
The transform() function in R is used to create new variables or modify existing variables in a data
frame
example :
1 # Use the transform() function to add a new variable "Total"
2 Data_Frame <- transform(Data_Frame, Total = R_note + Math )
3
4 # Print the data frame with the new variable "Total"
5 print(Data_Frame)
9. Cross-tabulation
A cross-tabulation is a way to summarize and organize data from a data frame based on two (or more)
variables
example:
1 Data_Frame <- data.frame(
2 Gender = c("Male", "Female", "Male", "Female", "Male"),
3 Specialization = c("GSI", "ACG", "GSI", "GSI", "ACG")
4)
5
6 # Create a cross-tabulation
7 cross_tab <- table(Data_Frame$Gender, Data_Frame$Specialization)
8
9 # Print the cross-tabulation
10 print(cross_tab)
33
Chapter 4: Data Frames (data.frame)
Graphic 20 Cross-tabulation
34
Exercise solutions
[exercice p. 22]
Solution n°1
___________________
Perform the solution directly:
1 20000/26
2 round(20000/26, digits = 3)
___________________
another way is to ; Assign the value "20000/26" to a variable then round that variable:
1 A= 20000/26
2 round(A, digits = 3)
[exercice p. 22]
Solution n°2
1. Créer le vecteur «vec1» contenant la suite des entiers de 1 à 12.
1 vec1 <- 1:12
2 vec1<- (1:12)
3 vec1
or directly:
1 vec2=c(vec1, 16:18)
2 vec2
[exercice p. 22]
Solution n°3
We can create a sequence using the `seq()` function. Firstly, we observe that the step of the sequence
is 0.5. Therefore, we can generate a sequence with a step of 0.5 in two different ways.
1 seq(0,5,0.5)
2 seq(0, 5, by = 0.5)
Another method involves specifying the length of the vector, with "R" automatically computing the
step (0.5).
Since our vector's length is 11 (indicating 10 steps to be added), and the vector ends with 5, the step is
computed as 5/10 = 0.5.
1 seq(0, 5, length.out = 11)
To obtain help regarding the length.out parameter in the seq() function, you can simply execute
the following code:
1 help(seq)
35
Exercise solutions
[exercice p. 22]
Solution n°4
Perform the solution directly using a sequence by a step of 2:
1 vec3 = seq(2, 52, by = 2)
2 vec3
[exercice p. 23]
Solution n°5
We can simply use the paste() function to concatenate elements together.
this function (paste()) will combines the string "individu" with the sequence of integers from 1 to
100.
The sep = " " argument specifies that a space should be used to separate the elements.
1 vec5 = paste("individu", 1:100, sep = " ")
2 vec5
to get help about the past function simply type and compile!
1 ?paste
[exercice p. 23]
Solution n°6
1-Extract the elements from the 1st to the 3rd position
1 SPE =c("GSI", "ACG", "E-BUSINESS", "DFBM", "MD")
2 SPE1=SPE[c(1,3)]
3 SPE1
36
Exercise solutions
[exercice p. 23]
Solution n°7
1-Without separator ""
1 VE = paste("chr", c(1:22,"X","Y"),sep="")
2 VE
[exercice p. 23]
Solution n°8
1-Find the length of NOTES
1 NOTES= c(15, 20, 5, NA, 12, 10, 8, 16, 18, 6, 3, 20, NA, 14, 9)
2 length(NOTES)
37
Bibliography
[1] Chitrangada, Chaubey., Anuska, Sharma. (2023). The integrated development environment (IDE) for
application development: Android studio and its tools. Nucleation and Atmospheric Aerosols, doi:
10.1063/5.0116494
[2] V., Narendra, Siva, Kesava., P., Lalitha, Likitha., B., Siva, Sai., K., Praveen., K., Sravani, Bai., G.,
Srinivasa, Rao. (2022). An Online Integrated Development Environment (IDE) by usingLightweight
Framework Render Studio. International Journal of Scientific Research in Computer Science, Engineering
and Information Technology, doi: 10.32628/cseit228386
38
Legal notices
Offered by Dr. Mohamed BOUATELLI & Dr. Rachid AZZAZ at ESGEN1 School, this beginner-level course
focuses on R programming2
-Ownership: All course materials are the intellectual property of Dr. Mohamed BOUATELLI, Dr. Rachid
AZZAZ, and ESGEN School. For inquiries or permission requests, please contact Dr. Mohamed BOUATELLI
and Dr. Rachid AZZAZ or ESGEN School.
Dr. Mohamed BOUATELLI - [email protected]
1. https://www.esgen.edu.dz/
2. This course is available in SCORM, Web, and PDF formats at: -
https://elearning.esgen.edu.dz/moodle/course/view.php?id=236
39