This Edureka R Tutorial (R Tutorial Blog: https://goo.gl/mia382) will help you in understanding the fundamentals of R tool and help you build a strong foundation in R. Below are the topics covered in this tutorial:
1. Why do we need Analytics ?
2. What is Business Analytics ?
3. Why R ?
4. Variables in R
5. Data Operator
6. Data Types
7. Flow Control
8. Plotting a graph in R
2. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Agenda
➢ Why do we need Analytics ?
➢ What is Business Analytics ?
➢ Why R ?
➢ Variables in R
➢ Data Operator
➢ Data Types
➢ Flow Control
➢ Plotting a graph in R
6. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
What is Business Analytics?
➢ Business analytics examines large and different types of data to uncover hidden patterns, correlations
and other insights.
Data Analytics Decisions
7. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
What is Data Visualization?
➢ Visualization allows us visual access to huge amounts
of data in easily digestible visuals.
➢ Well designed data graphics are usually the simplest
and at the same time, the most powerful.
10. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Why R?
Programming and Statistical Language
Data Analysis and Visualization
Apart from being used as a statistical language , it can also be
used a programming language for analytical purposes.
Apart from being one of the most dominant analytics tools, R also is
one of the most popular tools used for data visualization.
11. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Why R?
Simple and Easy to Learn
Free and Open Source
R is a simple and easy to learn, read & write
R is an example of a FLOSS (Free/Libre and Open Source Software)
which means one can freely distribute copies of this software, read it's
source code, modify it, etc.
16. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
X = 25
Y <-
“Hello”
TRUE -> B
B = TRUE
Y = Hellp
X = 25
Memory
Variables 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.
19. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Arithmetic Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Special Operators
1
2
3
4
5
+
Add two operands or unary plus
-
Subtract two operands or unary
subtract
*
Multiply two operands
/ Divide left operand with the right and result
is in float
>> 2 + 3
5
>> +2
>> 3 – 1
2
>> -2
>> 2 * 3
6
>> 6 / 3
2.0
20. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Arithmetic Operators
^
Left operand raised to the power of right
%% Remainder of the division of left operand
by the right
%/% Division that results into whole number
adjusted to the left in the number line
>> 2 ^ 3
8
>> 5 %% 2
1
>> 7 %/ %3
2
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Special Operators
1
2
3
4
5
21. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Assignment Operators
Relational Operators
Logical Operators
Special Operators
3
4
5
Arithmetic Operators1
Assignment Operators2
=
x = <right operand>
<−
x <- <right operand>
<<−
x <<- <right operand>
->
<left operand> -> x
>> x=5
>>x
5
>> x<- 15
>> x
15
>> x <<- 2
>> x
2
>> 25 -> x
>> x
25
22. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Relational Operators
> True if left operand is greater than the right
<
True if left operand is less than the right
==
True if left operand is equal to right
!=
True if left operand is not equal to the right
>> 2 > 3
False
>> 2 < 3
True
>>2 == 2
True
>> x >>=
2
>>print(x)
1
Logical Operators
Special Operators
4
5
Arithmetic Operators1
Assignment Operators2
Relational Operators3
23. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Relational Operators
>= True if left operand is greater than or equal
to the right operand
=<
True if left operand is less than or equal to
the right operand
>> 2 > =3
False
>> 2 =< 3
True
Logical Operators
Special Operators
4
5
Arithmetic Operators1
Assignment Operators2
Relational Operators3
24. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Logical Operators
Special Operators5
Arithmetic Operators1
Assignment Operators2
Relational Operators3
Logical Operators4
&
Returns x if x is False , y otherwise
|
Returns y if x is False, x otherwise
!
Returns True if x is True, False otherwise
>> 2 & 3
3
>> 2 | 3
2
>> ! 1
False
25. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Special Operators
: It creates the series of numbers in sequence
for a vector
%in%
This operator is used to identify if an element
belongs to a vector.
>> x <- 2:8
>> x
[1]2 3 4 5 6 7 8
Arithmetic Operators1
Assignment Operators2
Relational Operators3
Logical Operators4
Special Operators5
>> x <- 2:8
>> y <- 5
>>y %in% x
True
28. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
➢ A Vector is a sequence of data elements of the same basic type.
Example:
vtr = (1, 3, 5 ,7 9)
or
vtr <- (1, 3, 5 ,7 9)
➢ There are 5 Atomic vectors, also termed as five classes of vectors.
30. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ Replacing
list[2]<- “f’ “a” , “f” , “c” ,”d”
Sequence Operations:
➢ Indexing
list <- c(“a” , “b” , “c” ,”d”) List(2:4) “b” “c” “d”
list <- c(“a” , “b” , “c” ,”d”)
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
➢ sort()
list <- c(4 , 6, 3, 8, 1) Sorted<- sort(list) 1 3 4 6 8
31. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ Lists are the R objects which contain elements of different types like
− numbers, strings, vectors and another list inside it.
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
>x = list(n, s, TRUE)
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
32. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
Sequence Operations:
➢ Merging
➢ Slicing
➢ Indexing
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue") merged.list <- c(list1,list2)
1 2 3
Sun Mon Tue
list1 <- list(1,2,3) string1[-1] + string[1] ‘da’
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")
List3 <- c(list1, list2)
List3[2] Sun Mon Tue
33. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ Arrays are the R data objects which can store data in more than two dimensions.
➢ It takes vectors as input and uses the values in the dim parameter to create an
array.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
result <- array(c(vector1,vector2),dim = c(3,3,2))
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
34. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ Matrices are the R objects in which the elements are arranged in a two-
dimensional rectangular layout.
➢ A Matrix is created using the matrix() function.
matrix(data, nrow, ncol, byrow, dimnames)
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
❖ data is the input vector which becomes the data elements of the matrix.
❖ nrow is the number of rows to be created.
❖ ncol is the number of columns to be created.
❖ byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
❖ dimname is the names assigned to the rows and columns.
35. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ Factors are the data objects which are used to categorize the data and store it as
levels
➢ They can store both strings and integers.
➢ They are useful in data analysis for statistical modeling.
data <- c("East","West","East","North","North","East","West","West“,"East“)
factor_data <- factor(data)
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
36. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Type
➢ A data frame is a table or a two-dimensional array-like structure in which each
column contains values of one variable and each row contains one set of values
from each column.
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
emp.data <- data.frame(emp_id, emp_name, salary)
Arrays
Vectors
Lists
Matrices
Factors
Data Frames
40. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Flow Control
SELECTORS CHOOSE ACTIONS
SO YOU DON’T HAVE TO ...
if .. else
if It evaluates a single
condition
It evaluates a group of
conditions and selects the
statements
switch
It checks the different
known possibilities and
selects the statements
42. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Flow Control
2. if…else statement
Syntax:
if (condition 1):
statements 1 …
.
.
.
else
statements n …
If code
End
Start
If
Condition
FALSE
TRUE
FALSE
Else If
Condition
TRUE
Else If code
Else code
43. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Switch
START
Case1
Case2
Statement1
default
Statement2
Default
Statement
FALSE
TRUE
FALSE
TRUE
3. Switch statement
Syntax:
switch (expression,
value1: Statement1
value2: Statement2
.
.
, default Statement
)
End
Flow Control
45. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Flow Control
LOOPS REPEAT ACTIONS
S O YO U D O N ’ T H AV E TO . . .
While
Repeat
Repeat things until the
loop condition is true
Repeat things until the
loop condition is true
For Repeat things till the
given number of times
48. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Check
condition
Initialization
START
Execute Statements
Exit loop
repeat
FALSE
TRUE
6. for statement
Syntax:
for(value in vector)
{
statements…
}
Flow Control
50. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Check Loop
Condition
START
repeat
EXIT LOOP
Check next
Condition
Execute Block 2
8. next
TRUE
FALSE
FALSE
TRUE
Syntax:
next;
Execute Block 1
Flow Control
52. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization in R
➢ Data Visualization helps the organizations unleash the power of their most valuable assets: their
data and their people.
Data Visualization
Pie Chart Bar Chart Boxplot Histogram Line Graph Scatterplot
53. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization
Boxplot
Pie Chart
Bar Chart
Histogram
Line Graph
Scatterplot
➢ Pie charts are best to use when you are trying to compare parts of a
whole.
54. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization
Boxplot
Pie Chart
Bar Chart
Histogram
Line Graph
Scatterplot
➢ Bar graphs are used to compare things between different groups or
to track changes over time.
55. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization
Boxplot
Pie Chart
Bar Chart
Histogram
Line Graph
Scatterplot
➢ Boxplot are used summarize data from multiple sources and display
the results in a single graph.
56. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization
Boxplot
Pie Chart
Bar Chart
Histogram
Line Graph
Scatterplot
➢ Histogram are used to plot the frequency of score occurrences in a
continuous data set that has been divided into classes, called bins.
57. https://www.edureka.co/r-for-analyticsEDUREKA DATA ANALYTICS WITH R CERTIFICATION TRAINING
Data Visualization
Boxplot
Pie Chart
Bar Chart
Histogram
Line Graph
Scatterplot
➢ Line graphs are used to track changes over short and long periods
of time.