0% found this document useful (0 votes)
11 views31 pages

Introduction to R and RStudio Basics

R programming language
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views31 pages

Introduction to R and RStudio Basics

R programming language
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to R

Applied Statistical Methods (STG 09209)


Advanced Statistical Methods (AGS 606)

Leguma Bakari
Email: [Link]@[Link]
Email: leguma2020@[Link]
Phone:+255 762 760 095
February 20, 2024
Eastern Africa Statistical Training Center (EASTC)

1
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

2
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

3
R Software Definition

• R is a programming language and software environment for


statistical analysis, graphics representation and reporting, or
• R is a popular programming language used for statistical
computing and graphical presentation.
• R was created by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand, and is currently developed
by the R Development Core Team.
• R is freely available under the GNU General Public License, and
pre-compiled binary versions are provided for various operating
systems like Linux, Windows and Mac.
• This programming language was named R, based on the first
letter of first name of the two R authors (Ross Ihaka and Robert
Gentleman).
4
R Studio Definition

• RStudio is an integrated development environment (IDE) for R.


• It includes a console, syntax-highlighting editor that supports
direct code execution, as well as tools for plotting, history,
debugging and workspace management.
• RStudio is available in open source and commercial editions and
runs on the desktop (Windows, Mac, and Linux) or in a browser
connected to RStudio Server or RStudio Server Pro
(Debian/Ubuntu, RedHat/CentOS, and SUSE Linux).
• RStudio is a free and open-source integrated development
environment (IDE) for R, a programming language for statistical
computing and graphics. JJ Allaire, creator of the programming
language ColdFusion, founded RStudio.
• Hadley Wickham is the Chief Scientist at RStudio.
5
R and RStudio: What is the difference?

• First time users of R are often confused as to the difference


between R and RStudio.
• RStudio is actually an add-on to R: it takes the R software and
adds to it a very user-friendly graphical interface.
• Thus, when one uses RStudio, they are still using the full version
of R while also getting the benefit of greater functionality and
usability due to an improved user interface.
• As a result, when using R, one should always use RStudio;
working with R itself is very cumbersome.
• Since RStudio is an add-on to R, you must first download and
install R as well as RStudio, two steps which are done separately.
• On your computer, you will see R and RStudio as separate
installed programs.
6
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

7
R and RStudio Installation

• As discussed above, you must download and install both R and


RStudio.
• To install R, click here and download the latest version of R for
Windows, Mac or Linux.
• When you have downloaded and installed R, you can run R on
your computer.
• To install Rstudio, click here for Windows, Mac, and Linux
(Ubuntu).
• Scroll due to Installers for Supported Platforms, open the chosen
platform’s link, run the installer after downloading, and follow
the instructions.

8
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

9
The interface of RStudio

When you open R Studio for the first time, you should see the
following interface:

10
Console

• The large window on the left side is the Console.


• You can think of this as the “calculator” for R Studio.
• This is were all of the input, calculations and output are
contained.
• In fact, if you were to run R and not R Studio, this Console is the
only window you would see;
• The > symbol indicates the current line in the Console, with the
pointer at this line indicated by a blinking vertical bar.
• In R, each command is considered a single line;
• you cannot have multiple commands on the same line unless you
separate the commands in the single line with a semicolon (;).
• Try typing 2+2;3+3 in the Console and view the output.

11
R Scripts

• While the Console forms the workhorse of R, operating solely in


the Console is very cumbersome.
• Instead of typing your commands in the Console each time you
run R, we will instead create a script.
• A script is a list of R commands that is saved as a text file to then
be submitted into R line by line.
• Scripts are what makes R so useful as they allow easy
reproducibility.
• Scripts are saved as a .R extension which can be read by most
text editors (e.g., Notepad in Windows).
• To create a new script you can select File > New File > R Script
at the top of R Studio.
• You should see the Console window go to the bottom and a new,
empty window appear above the Console. 12
• This new window is where your scripts that are open will appear;
each script that you have loaded is marked by a tab at the top of
the window.
• Anytime you change a script, the text becomes red and an *
appears to indicate that it has been edited since it was last saved. 13
R Studio Shortcuts

1 ctrl + 1 (ctrl + shift + 1) for r script window


2 ctrl + 2 (ctrl + shift + 2) for console window
3 ctrl + 3 (ctrl + shift + 3) for help window
4 ctrl + 4 (ctrl + shift + 4) for history window
5 ctrl + 5 (ctrl + shift + 5) for files window
6 ctrl + 6 (ctrl + shift + 6) for plots window
7 ctrl + 7 (ctrl + shift + 7) for packages window
8 ctrl + 8 (ctrl + shift + 8) for environment window
9 ctrl + 9 (ctrl + shift + 9) for viewer window
10 ctrl + alt + R for history of executed syntaxes
11 ctrl + shif + N for new R script
12 ctrl + shift + P for file menu
13 ctrl + shift + R for for inserting section in active R script
14
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

15
Why Use R?

• It is a great resource for data analysis, data visualization, data


science and machine learning,
• It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction),
• It is easy to draw high quality graphs in R, like pie charts,
histograms, box plot, scatter plot, etc++
• It works on different platforms (Windows, Mac, Linux),
• It is open-source and free.
• It has a large community support, and
• It has many packages (libraries of functions) that can be used to
solve different problems.

16
Important Tips for R Beginners

1 R is an open source platform


2 R is built on packages/libraries
3 There are multiple ways of doing the same thing in R
4 R Packages can change
5 R is case sensitive
6 R can import data from different platforms, from excel, SPSS,
Stata and many more.

17
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

18
Data Operators in R

There are five (5) different operators in R programming.

i Arithmetic Operators
ii Assignment Operators
iii Relational Operators
iv Logical Operators
v Special Operators

19
Arithmetic Operators

Description Operator Example


Adding up two or more operands + 3+2=5
Subtracting two or more operands - 3-2=1
Multiplying two or more operands * 3*2=6
Dividing two or more operands / 3/2=1.5
Exponentiation of operand ˆ 3ˆ2=32 = 9
Exponentiation of operand ** 3**2=9
Reminder for the division %% 5%%2=1
Quotient(whole number) for the division %/% 5%/%2=2

20
Assignment Operators

• The assignment operators are used to assign values,characters or


any other type to the object.
• The operator can either be left or right assignment operator.
• There are about five assignment operators.

Description Operator Example


= a=5
<− b< −5
Left Assignment
<< − d<< −5
−> 5− >e
Right Assignment − >> 5− >>f

21
Relational (Conditional) Operators

Description Operator Example


TRUE if the left operand is grater than the > 2>3
right
TRUE if the right operand is less than the left < 2<3
TRUE if the right operand is equal to the left == 2 == 3
TRUE if the right operand is not equal to the != 2! = 3
left
TRUE if the left operand is grater than or equal >= 2 >= 3
to the right
TRUE if the right operand is less than or equal <= 2 <= 3
to the left

22
Logical Operators

Three symbols are the famous one for logical operators for AND,OR
and NOT (negation)

Description Operator Example


AND & TRUE & TRUE==TRUE
OR | TRUE | FALSE==TRUE
NOR ! !FALSE==TRUE

23
Special Operators

Description Operator Example


Creation of of number series in : x< −2 : 9
sequence
Identify if an element belong to %in% 5 %in% x
a vector
Multiplying a matrix A and ma- %*% M1 = A %*% B
trix B

The above example is like asking if 5 is among the element of vector x.

24
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

25
R Comments

• Comments can be used to explain R code, and to make it more


readable.
• It can also be used to prevent execution when testing alternative
code.
• Comments starts with a #.
• When executing the R-code, R will ignore anything that starts
with #
• This example uses a comment before a line of code:
• Example;
• # This is a comment
• To comment multi-line, highlights the intended lines then use
shortcut Ctrl + Shift + C
• You can just as easily remove the comment by highlighting the
lines again and pressing the same shortcut Ctrl + Shift + C 26
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

27
Data Types and Variables

• Generally, while doing programming in any programming


language, you need to use various variables to store various
information.
• In R variables are nothing but reserved memory locations to store
values.
• This means that, when you create a variable you reserve some
space in memory.
• The variables are assigned with R-Objects and the data type of
the R-object becomes the data type of the variable.
• There are many types of R-objects,

28
• The frequently used ones are
i Vectors
ii Matrices
iii Arrays
iv Factors
v Lists
vi Data Frames
vii Date
• The simplest of these objects is the vector object and there are six
data types of these atomic vectors, also termed as six classes of
vectors.
• These atomic vectors includes Logical, Numeric, Integer,
Complex, Character, and Raw

29
Outline

1 Introduction
R and Rstudio Definitions
R and RStudio Installation
RStudio Interface
2 Why Use R?

3 Data Operators in R

4 R Comments
5 Data Types and Variables

6 Frequently Used Functions

30
Frequently Used Functions

• str(object) structure of an object


• class(object) class or type of an object
• names(object) names
• c(object,object,...) combine objects into a vector
• cbind(object, object, ...) combine objects as columns
• rbind(object, object, ...) combine objects as rows
• object() prints the object
• ls() list current objects
• rm(object) delete an object
• rm(list=ls()) delete an objects in memory
• names(object) print names for current objects
• edit(object)->newobject edit copy and save as newobject
• fix(object) edit in place
• levels(object) list levels of factor object.
• length(object) number of elements or components
• lengths(object) number of elements within objects
• dim(object) dimensions of an object
• ncol(object) number of columns of a matrix or data frame object
• nrow(object) number of rows of a matrix or data frame object
• head(mydata, n=10) print first 10 rows of mydata
• tail(mydata, n=5), print last 5 rows of mydata
• args(function), Shows arguments of a function

31

You might also like