INTRODUCTION TO R
TUTORS AIMS-GHANA 01 & 03 NOV , 2023
OUTLINE
01 02 03
SESSION SET-UP WHY R GETTING R
R popularity, R vs. others, R How to install, R & RStudio, How to
What to expect, session
capacities, pros and cons run R programs,
objectives, planned activities
04 05 06
INTRO TO CORE R SELF LEARNING END OF DAY
FEATURES EXERCISE
How to keep learning R on your own
Key language features
SESSION OBJECTIVES Expected Learning Outcomes
At the end of the session, learners should be able to:
01 PERFOM BASIC COMPUTATIONS IN R
Create variables, compute with variables and more
02 WORK WITH ESSENTIAL DATA TYPES IN R
Numeric, Strings, Lists, Vectors, Data Frames
03 USE R’S CONTROL FLOWS
If statements, for and while loop, break and continue
04 WRITE FUNCTIONS , CREATE MODULES AND PACKAGES
Write functions, create modules and package python code
05 HANDLE FILES
Read from files, write to files and use module to retrieve files
06 DEBUGG CODE AND HANDLE ERRORS AND EXCEPTIONS
Session Setup
How We will Do it
For each topic
Introductory presentation Follow along coding Exercise and practice
Provide introduction to key concepts See how each concept works using toy Tackle more realistic problems
examples
4 hrs. 2 hrs.
5
WHY R?
7/18/2017 Big Data Program-Lighting Talks
About R
Object
Oriented
• R is an interpreted, object-oriented, high-
level programming language with dynamic
semantics
• R is a programming language and free
software for statistics and data science Procedure High-level
Oriented language
• R was created by Ross Ihaka et Robert
Gentleman in 1993
• R is very easy to learn
Easy to lean
Who Uses R?
Some of the Well Known Companies Who Use R
Some Well Known Companies Using R
The R advantage
1. Popular with large user community and open libraries
2. Free /open source
3. A general-purpose programming language
4. Easy to learn
Popularity of R has been increasing steadily
R Vs. STATA/SPSS
STATA, SPSS $ Price
User community
Sharing of code
Specialized stats and econ
modules
Technical support
Machine Learning
Large and
unstructured data
Key R Features
How easy is R?
Simple, Easy and Concise
• R code is easy to read and write
R
• The language is easy to learn
Free and Open Source
R is an example of a FLOSS (Free/Libre and Open Source
Software) which means one can freely distribute copies of
STATA
this software, read it's source code, modify it, etc.
Portable
Supported by many platforms like Linux, Windows,
FreeBSD, Macintosh Python
How to Get and Install R
Install essential Use R
Install R for your OS
Libraries/Packages
[Link] from : R is powered by thousands of
[Link] user contributed and freely
available packages which can be
2. Rstudio: installed easily using command :
[Link]
dio-desktop/ [Link](“package_name”)
3. Other third party platforms
R 4.3.0 Vs. R 4.2.3
• R 4.3.: R 4.3.x
• Why are they 2 versions: R 4.2 is legacy, so R
4.3 is the future.
• There are syntactical differences between the
two.
Rstudio can be installed with either R 4.2 or 3. We will use R 4.3. in this course.
R Development ENvironments
Development Environment: a place where you write and/or run R program
Interactive R Interpreter on the Interactive Development
command line Environment (IDEs)
1. Terminal/Shell-Command line 1. IDLE – default for R
2. Sypder (similar to R studio)
3. Rstudio /R Notebook
4. Jupyter Notebook
HELLO WORLD!
R first program in different coding environments.
R Program Environments
1. Basic Terminal
print(“Hello World”)
2. Sypder (similar to R studio)
3. Rstudio /R Notebook
4. Jupyter Notebook
15
A quick Introduction to Rstudio
7/18/2017 Big Data Program-Lighting Talks
Installing and Using Rstudio App
The RStudio App doesn’t come with R, you have to install it- we will see how to install later
Run the codes
Environment
for viewing
objects
Scripts/Cod
e
Console
Install
Packages/Vi
sualization
What is RStudio?
A graphical interface application in which you can create and share documents that contain live
codes, equations, visualizations and text.
Basic Features
• The RStudio application produces documents that we
call "scripts", "R Notebooks" or "R markdown
documents", which contain both code and rich text
elements, such as figures, links, equations, etc.
• RStudio is a free, open-source, cross-platform development
environment for R, a programming language used for data
processing and statistical analysis.
• The RStudio application is a server-client application
that can be run on a PC without Internet access, or
installed on a remote server, which you can access via
the Internet.
• The RStudio application allows you to edit and run your
notebooks via a web browser.
What to Know about Rstudio App/Notebook
We will quickly run through the following concepts which are useful for this course.
• Launching the App
• File management stuff
• Opening an existing scripts, creating a new,
Saving/renaming/duplicating/deleting a file
• Saving notebook
• Exporting notebook
• Cells:
• Creating, inserting, deleting and moving cells
• Execute a cell, stop execution, execute many cells, etc
• Code cells vs text cells
• Knowing when a cell is executing
• Kernels
• Interrupt/stop/restart/shutdown kernel
• Change kernel
• Tips
• Getting help
• Tab completion
• Shortcuts
Exercise: Familiarize Yourself with the Rstudio
Time: 5 minutes
For new users
1. Launch RStudio
2. Create a new file (Rscript) using RStudio and save
it as ‘Exercice_1’.
3. Create a new code cell/line and do a math
calculation : “5 + 5”. Run the cell/line
4. Try this time consuming computation :
a=seq(10000000) for(i in a){print(i)}. What’s
happening? Stop the execution.
A Quick run Through R core Features
01 OPERATORS
Assignment, arithmetic and more
02 VARIABLES AND DATA TYPES
Strings, Numeric, container and more
03 CONTROL FLOW
Conditional statements, and loops
04 FUNCTIONS, MODULES AND PACKAGES
Defining and calling functions, creating modules
05 FILE HANDLING
Reading, writing files, using the module
06 ERROR HANDLING AND DEBUGGING
Type of errors, handling errors,
debugging
General R Features
Comments
Start comments with # - the rest of line is ignored.
•Can include a “documentation string” as the first line of any new
function or class that you define.
Variable Naming Rules
• Names are case sensitive and cannot start with a number. They can
contain letters, numbers, and underscores.
• Reserved words: sum, for, if, paste, continue, class, etc.
22
R Operators
7/18/2017 Big Data Program-Lighting Talks
Arithmetic Operators
Operator Exemple
ADDITION (+)
Add two operands or unary plus
SUBTRACTION (-)
Subtracts two operands
MULTIPLICATION (*)
Multiplies two operands
DIVISION (/)
Divide left operand with the right and result is
in numeric
EXPONENTION (**)
Left operand raised to the power of
right
REMAINDER (%%)
Remainder of the division of the left operand by the right
Assignment Operators
•The first assignment to a variable
creates it.
• Variable types don’t need to
be declared.
• R figures out the variable
types on its own.
• Assignment uses =
Comparison Operators
Operator Exemple
GREATER (>)
True if left operand is greater than right
LESS THAN (<)
True if left operand is less than right
EQUAL TO (==)
Tue if left operand is equal
to right
NOT EQUAL TO ( !=)
True if left operand is not equal
to right
Logical Operators
Operators & and | have expected definitions
&
If both the operands are true then
condition becomes true.
|
True if left operand is less than right.
27
ENOUGH TALK LETS
CODE
7/18/2017 Big Data Program-Lighting Talks
28
R Data Types
7/18/2017 Big Data Program-Lighting Talks
Overview of R Data Types
Data Type
Immutable Mutable
Numeric, Character/ Logical/boo Vectors, Lists Data
Integer, Strings lean Matrices Frames
Complex Arrays
What You need to Know About Data types
• How to create them. [Almost all]
• How to convert between types. [Almost all]
• How to modify them (if mutable) [List, dicts, etc]
• How to index, slice, etc. [Strings, Lists, Arrays, etc]
• How to loop through them. [Lists, string, dicts, data frame]
• How to use them with data science libraries (e.g., readxl) [Lists, dicts, data
frame, arrays, etc.]
• How to add/remove/replace items. [Lists, strings, dicts et autres types
mutables]
• How to check memership. [Lists, dicts, etc.]
• How to count number of items. [Lists, dicts, strings]
A Few Notes About Types in R
The class() can be used to check a
variable/object data type
Although R does some data type conversion internally (e.g., converts numbers from
strings) sometimes, you need to coerce a data type explicitly from one type to another
to satisfy the requirements of an operator or function parameter.
Numbers in R
Number data types store numeric values. They are immutable data types, means that changing
the value of a number data type results in a newly allocated object.
Common Numerical Types in R Number Operations Number Type Conversions
• Numeric: numerics are positive or
negative whole numbers with no
decimal point.
• Integer: This is used when you are •Type numeric(x) to
certain that you will never create a convert x to a plain
variable that should contain decimals. complex
To create an integer variable, you
must use the letter L after the integer •Type complex(x) to
value. covert to a long integer
• Complex: A complex number is
written with an "i" as the imaginary •Type integer(x) to
part. convert to a numeric.
Strings
Strings are simple form of R sequence. They are stored as a collection of individual characters.
Common string Operations
Creating variables:
• single(’) or double (") quotes
• nchar(string) : to find the number of
Assign a String to a Variable: characters in a string
• grepl(string) –to check if a character or
• Assigning a string to a variable is a sequence of characters are present
done with the variable followed in a string
by the <- operator and the string
• paste(string): to merge/concatenate
• Chaînes multilignes
two strings.
• assigner une chaîne de plusieurs
lignes à une variable de la
manière suivante :