Professional Documents
Culture Documents
Ba 340: Data Analytics: Understand How R Stores and Works With Data
Ba 340: Data Analytics: Understand How R Stores and Works With Data
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?
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
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
1st
dim
2nd dim
Matrices
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"
"!"
"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)
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)
# "5", "two"
c(TRUE, "a")
# "TRUE", "a"
c(TRUE, FALSE, 3)
# 1, 0, 3
Manual Coercion
Lists and data frames