Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

BA 340 : DATA ANALYTICS

Understand how R stores


and works with data

Course By : Nabil Chaabane


Outline

1. Rstudio basics

2. R basics

3. Data Structures

4. Data types
How to Install R Studio

1. Windows https://cran.r-project.org/bin/windows/base/

2. Macintosh https://cran.r-project.org/bin/macosx/

3. Linux https://www.digitalocean.com/community/tutorials/how-to-
install-r-on-ubuntu-18-04-quickstart

Attention!! Verify which version of Linux you are using when installing R
Studio
The RStudio window is divided into 3-4 panes. Each keeps track
of separate information.
R console
The console gives you a
place to execute commands
written in the R computer
langage.
R prompt
Type commands on the line
that begins with a > sign
(known as the prompt).
Output
When you hit enter, RStudio
will run your command and
display any output below it

Output

New prompt
History
As you enter commands,
you accumulate a history of
past commands
[1]
R displays an index next to
the output. When an
expression returns one
output, you will see [1]. This
index is helpful if the
expression returns more than
one output.
+ prompt
When an expression is
incomplete, R prompt will
return a + sign. Either finish
the expression or press
escape.
+ prompt
When an expression is
incomplete, R prompt will
return a + sign. Either finish
the expression or press
escape.
Workflow

1. What if you have several commands you’d like to execute and you
would like to save your work?

2. R scripts make your life easier.

3. To open an R script, go to File>New File>R Script in the toolbar.


R script pane
Step 1: Write
code in a R
script
Step 2: Run
code in console
with run
Step 3: Save
code
R objects
A simple code :
1. Add 5 and 2

2. Multiply the result by 3

3. Divide the latter by 4

4. Compute the log of the output


Save information as an R object with the
smaller than sign followed by a minus
sign, e.g, an arrow
Information to store
Name of the new in the object
object

x <- 1

Assignment operator,
“gets”
When you create an R object, you'll see it appear in
your environment pane
Object names

Object names cannot begin with a number e.g.
1step


Object names should not contain special characters
e.g. d$


Do not use names already in use, e.g. mean,
median


Use meaningful names !!
Object names

R will treat each of these as a different object

a A
b B
x X

You can remove an object using rm
Data
structures

There is an R object called WorldPhones. Let’s take
a look at it

One can store more than one information in a


variable
Vectors
Combine multiple elements into a one
dimentional array.
Create with the c function
Matrices
Combine multiple elements into a two
dimentional array.
Create with the matrix function
Matrices
Number of rows of
Vector of elements to the matrix
go in the matrix

1st
dim

2nd dim
Matrices

matrix(c(1, 2, 3, 4, 5, 6,7,8,9,10,11,12), nrow = 3)


Matrices
Algebra
Vector Multiplication in R

Inner product


Outer product
Matrix operations in R

Transpose of a matrix


Matrix multiplication
Arrays
Data Types
Data Types
R recognizes different types of data. We will focus
on four basic types :

Numbers.


Strings.


Logical.


Factor.
Numeric
Any number, no quotes.

1
3000000
class(0.00001)
# "numeric"

These are not a numerics "1", "7"


Character
Any symbols surrounded by quotes.

Appropriate for words, variable names,


messages, any text.

"!"
"hello"
class("hello")
# "character"
class("4")
# "character"
Character
"hello" + "world"
# Error

nchar("hello")
#5

paste("hello", "world")
# "hello world"
Logical
TRUE or FALSE (T or F)

R's form of binary data. Useful for logical tests.

3<4
# TRUE

class(TRUE)
# "logical"

class(T)
# "logical"
Factor
Factors save the data as well as the levels.
Quiz

x <- c(1, 2, 3)

What is the difference between x and "x" ?


Quiz

Make a vector that contains the number 1,


the letter R, and the logical TRUE.

What class of data is the vector?


Answer
c(5, "two")

# "5", "two"

c(TRUE, "a")

# "TRUE", "a"
c(TRUE, FALSE, 3)

# 1, 0, 3
Manual Coercion
Lists and data frames

Lists and data frames generalize vectors and


matrices to allow multiple types of data.
Lists
Lists are a one dimentional group of R
objects.
Create lists with the list command
Lists
Data Frame

A data frame is a two dimensional group of


R objects.
Each column in a data frame can be of a
different type
Use data.frame to construct a data frame
Data Frame
Names
Names
Names
Names
Helper Functions

You might also like