You are on page 1of 41

Polytechnic University of the Philippines

Stat20023: Engineering Data Analysis

Data Types
Operators
Data Structures
Data Types
R Studio Tabs
Operators
Basic Tips
Data Structures

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects
Menu

Source Pane Environment Pane

Console Pane Files Pane

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects
Used to open a script
Source Pane

Console Pane

Environment Pane
Used in running a code. You can
Files Pane also run code by pressing
Ctrl+Enter
The source pane is a text
editor where you can
type your code before You can write a comment by adding # to an RScript
R will run the line where you placed your cursor
running it.

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane

Environment Pane

Files Pane To clear the console, you press


Ctrl+L or type cat(“\014”)
The console pane is the
interface to R. You can
type commands directly
in the console. The Command Line
console displays the
result of any command
you run.

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane
To remove all the objects from the environment
Console Pane

Environment Pane Environment tab displays any objects that you have
created during your R session
Files Pane
To see all the objects in the current session, type ls()
The Environment Pane
To remove a specific object use rm(…)
includes an Environment
tab, History tab,
Connections tab and
Tutorial tab

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane

Environment Pane History tab keeps a record of all the commands you have run

Files Pane To copy a command from the History into the console, select the
command and press Enter or click
The Environment Pane
includes an Environment If you want to copy the command into the script, select the
command and press Shift+Enter or click
tab, History tab,
Connections tab and
Tutorial tab

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane

Environment Pane

Files Pane
The Files tab displays the content of your working directory
The Files Pane includes You can find out which directory R is using by typing getwd() in the
several tabs that provide command line
useful information To change working directory type setwd(“H:/Preferred Directory”)
Notice that you need a forward slash (/) and not back slash (\)

If you do not see the contents of the file directory in the Files tab, click on
and then on

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane
The Plot tab shows all graphs that you have created
Environment Pane

Files Pane Click to export your plot as an image file or a pdf

To remove a single plot, click


The Files Pane includes
several tabs that provide To remove all plots, click or type dev.off() in the command line
useful information

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane The packages tab displays the R packages that you have installed in your System
Environment Pane Library. To install a package you can use the command install.packages(“package
name”) . Once you have it installed the package, you need to load it using the
Files Pane command library(package name).

An R package typically
includes code, data,
documentation for the
package and the function If there is a check mark beside the package, it means that it is already loaded
inside, and tests to check
everything works as it
should

Once a package is installed, you do not need to reinstall the package. If you want
to use a package, you have to load it every time you start a new RStudio session
Stat20023: Engineering Data Analysis
R Studio Tabs Basic Tips R Objects

Source Pane

Console Pane Function Name {Package Name}


Title
Environment Pane

Files Pane
The Usage section shows the function and the variables that
The Help tab has built-in need to be specified
documentation for
packages and functions in
R.

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Packages

Source Pane

Console Pane The Value section specifies what is returned by the function. R
documentation may also include references, a list of similar functions, and
Environment Pane examples.
Files Pane

The Help tab has built-in


documentation for
packages and functions in
R.
The Viewer tab displays
HTML output

Stat20023: Engineering Data Analysis


R Studio Tabs Basic Tips R Objects
R is case sensitive
A name cannot start with a number
A name cannot use some special symbols, like ^, !, $, @, +, -, /, or *

Good Names Bad Names

a 1trial
b $
F00 ^mean
my_var 2nd
.day !day

Stat20023: Engineering Data Analysis


Basic Data Types in R

Stat20023: Engineering Data Analysis


Basic Data Types
“a”
Character
“FALSE”
Numeric
“one”
Integer
Logical “integer”

Complex “negative”

Stat20023: Engineering Data Analysis


Basic Data Types
1.00
Character
5
Numeric
9.9999
Integer
Logical -7

Complex 0

Stat20023: Engineering Data Analysis


Basic Data Types
Append an L suffix
Character
-7L
Numeric
as.integer(4)
Integer
as.integer(3.14)
Logical
as.integer(“5.99”)
Complex

Stat20023: Engineering Data Analysis


Basic Data Types

Character
Numeric TRUE

Integer FALSE
Logical
Complex

Stat20023: Engineering Data Analysis


Basic Data Types

Character
z = 1 + 2i
Numeric
sqrt(-9+4i)
Integer
Logical sqrt(-25 + 0i)

Complex

Stat20023: Engineering Data Analysis


Basic Data Types

Character
z = 1 + 2i
Numeric
sqrt(-9+4i)
Integer
Logical sqrt(-25 + 0i)

Complex

Stat20023: Engineering Data Analysis


Character Numeric Integer Logical Complex
Functions to examine features of vectors and other objects

class() What kind of object is it (high-level)?

typeof() What is the object’s data type (low-level)?

How long is it? What about two dimensional


length()
objects?
attributes() Does it have any metadata?

Stat20023: Engineering Data Analysis


Operators

Stat20023: Engineering Data Analysis


Assignment operators are used to assign
a value to an object Operators

Assignment Operators =

Arithmetic Operators <- ->

Relational Operators Example


Logical Operators
x = 10 y<- 20 30 ->z

Stat20023: Engineering Data Analysis


Arithmetic operators are used to
perform basic mathematical operations
+ Addition
Assignment Operators
- Subtraction
Arithmetic Operators
* Multiplication
Relational Operators
Logical Operators / Division

Stat20023: Engineering Data Analysis


Relational operators are used to test/define a
< Less than
relationship between two operands

Assignment Operators <= Less than or equal to

Arithmetic Operators > Greater than

Relational Operators >= Greater than or equal

Logical Operators == Is equal to

!= Not equal to

Stat20023: Engineering Data Analysis


Logical operators are used to make decision on
| OR
the basis of a condition

Assignment Operators
FALSE FALSE FALSE
Arithmetic Operators
FALSE TRUE TRUE
Relational Operators
TRUE FALSE TRUE
Logical Operators
TRUE TRUE TRUE

Stat20023: Engineering Data Analysis


Logical operators are used to make decision on
& AND
the basis of a condition

Assignment Operators
FALSE FALSE FALSE
Arithmetic Operators
FALSE TRUE FALSE
Relational Operators
TRUE FALSE FALSE
Logical Operators
TRUE TRUE TRUE

Stat20023: Engineering Data Analysis


Data Structures

Stat20023: Engineering Data Analysis


Data Structures
Data structures are used to store data in an organized
fashion in order to make data manipulation and other data
operations more efficient.
• Vector
• List
• Matrix
• Array
• Data Frame
• Factor
Stat20023: Engineering Data Analysis
Data structures are used to store data in an organized fashion in order to
Data Structures make data manipulation and other data operations more efficient.

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame

Vector is a linear object which contains homogenous elements

c(1, 2, 3)

c(TRUE, FALSE)

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame

List is a linear object which contains heterogenous elements

list(101,”Stat”)

list(TRUE, 2+5i)

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame

Matrix is a 2-D object which contains homogenous elements

[ , 1] [ ,2 ] [ , 3]
matrix(c(1:6),nrow=2)
[ 1, ] 1 3 5

[ 2, ] 2 4 6

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame

Arrays are homogenous objects which have more than 2


dimensions

array(c(vector1, vector2),dim=c(3,3,2))

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame

Factors are objects which are used to categorize the data and
store it as levels

data<-c(“Male”, ”Female”, ”Female”, “Male”)

as.factor(data)

Stat20023: Engineering Data Analysis


Vector List Matrix Array Factor Data Frame
A data frame is a 2-D table where each column comprises of
homogenous elements and each row may contain either
homogenous or heterogenous elements
data.frame(Name=c(“Tim”, “Ace”), Age=c(24, 18))

Name Age
Tim 24
Ace 18
Stat20023: Engineering Data Analysis
Loading Data

Stat20023: Engineering Data Analysis


Loading Data into R
• Download and install the package readxl to read excel files
• Click “Import Dataset” in the Environment pane, then
select “From Excel”

Stat20023: Engineering Data Analysis


Loading Data into R
• Download and install the package readxl to read excel files
• Click “Import Dataset” in the Environment pane, then
select “From Excel”

Stat20023: Engineering Data Analysis


Loading Data into R
• Select the file
• Click Import

Stat20023: Engineering Data Analysis


References
• http://ncss-
tech.github.io/stats_for_soil_survey/chapters/1_introduction/1_introduction.html
• https://www.geeksforgeeks.org/data-structures-in-r-programming/
• https://www.tutorialspoint.com/r/r_matrices.html
• https://www.youtube.com/watch?v=NGGxJ754Q1c&t=781s
• https://www.youtube.com/watch?v=mcYcjH-1giM
• http://www.r-tutor.com/r-introduction/basic-data-types/logical

Stat20023: Engineering Data Analysis

You might also like