D1 2 Intro R
D1 2 Intro R
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
05 HANDLE FILES
Read from files, write to files and use module to retrieve files
4 hrs. 2 hrs.
5
WHY R?
Easy to lean
Who Uses R?
Some of the Well Known Companies Who Use R
4. Easy to learn
User community
Sharing of code
Technical support
Machine Learning
Large and
unstructured data
Key R Features
How easy is R?
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
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
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
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.
• 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
1. Launch RStudio
01 OPERATORS
Assignment, arithmetic and more
03 CONTROL FLOW
Conditional statements, and loops
05 FILE HANDLING
Reading, writing files, using the module
R Operators
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
• Assignment uses =
Comparison Operators
Operator Exemple
GREATER (>)
True if left operand is greater 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
R Data Types
Data Type
Immutable Mutable
• How to use them with data science libraries (e.g., readxl) [Lists, dicts, data
frame, arrays, etc.]
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.
A vector is a very useful data structure when handling data in R, so make sure you are comfortable
with it. Also in statistics, you are more likely to need vectors of numerical values.
Mathematical functions for vectors
In R, there are a large number of predefined mathematical functions that areuseful in practice. Here's a list of
functions for vectors:
• order().
• mean()
Try to understand each of the
• var() above functions. To do this, run
• min() RStudio, define some toy vectors,
• max() apply the above functions, and try
• which.min()
• which.max() to understand the output. If you
• sd() can't understand a function,
• median() consult the help function using the
• rep()
command help(“function_name”)
• seq()
or ?function_name
Matrices
A matrice is One of the mutable sequence types. It is the R object in which the elements are arranged in a two-
dimensional rectangular layout. A column is a vertical representation of data, while a row is a horizontal
representation of data
d =data.frame()
Remember that Data Frames can have different types of data inside it. While the first column can be
character, the second and third can be numeric or logical. However, each column should have the
same type of data..
38
R Control Flow
else…if if If…else
for (var)
{do something with var …}
while loop
syntax
while (var)
{do something with var …}
Functions in R
• Reduces errors
- function(item1, item2,…){
function body
}
Information can be passed into functions as arguments. Arguments are specified after the function
name, inside the parentheses. You can add as many arguments as you want, just separate them with
a comma.
47
2. Read files
3. Write to files
Exceptions
• Even if a statement or expression is
syntactically correct, it may cause an
error when an attempt is made to
execute it. Errors detected during
execution are called exceptions
• The last line of the error message
indicates what happened
• Exceptions come in different types, and
the type is printed as part of the
message
Remember, When You Get Stuck, You Can Always Ask Google
Resources for Learning R
Self Learning Crucial in Programming
1. Just Do It: Start using R for some of you
day to day data tasks
7. Start a R community