Business Analytics using R
Programming
Pravat Uprety
Ace Institute of Management
Central Department of Statistics
6/23/2024 Pravat Uprety 1
Business Analytics
• Business analytics, or simply analytics, is the use of data, information
technology, statistical analysis, quantitative methods, and
mathematical or computer-based models to help managers gain
improved insight about their business operations and make better,
fact based decisions.
• Business analytics is “a process of transforming data into actions
through analysis and insights in the context of organizational decision
making and problem solving.” Business analytics is supported by
various tools such as Microsoft Excel and various Excel add-ins,
commercial statistical software packages such as SPSS, STATA, SAS or
Minitab, and more complex business intelligence suites that integrate
data with analytical software (R, Python etc).
6/23/2024 Pravat Uprety 2
Data Organization
• In Data Science, statistics, econometrics, machine learning, and other
quantitative areas, a common organizational form is a rectangular
array consisting of rows and columns. The rows are typically objects
and the columns variables or features. An object can be a person
(e.g., a customer) or an event (e.g., a transaction). The words object,
case, individual, event, observation, and instance are often used
interchangeably
6/23/2024 Pravat Uprety 3
Data Organization
Respondent Age Gender Post Working
Department
6/23/2024 Pravat Uprety 4
Why R Programming
• R programming is very strong in statistics, so it is ideal for data
exploration or data understanding using descriptive statistics,
inferential statistics, regression analysis, and data visualizations.
• R is also ideal for modeling because you can use statistical learning
like regressions for predictive analytics
6/23/2024 Pravat Uprety 5
Why use R?
• It is open source
• Easy to learn
• Support good graphical capabilities
• Provide programmable graphics language
• Have active user groups where questions can be asked
• R has over 5500 packages available from different repositories
6/23/2024 Pravat Uprety 6
Why use R?
• R is a comprehensive statistical platform, offering all manner of data-
analytic techniques.
• R contains advanced statistical routines not yet available in other
packages. In fact, new methods become available for download on a
weekly basis.
6/23/2024 Pravat Uprety 7
What Is R?
• R is an independent, open-source, and free implementation and
extension of the S language, developed by an international team of
statisticians, including John Chambers. As described in Ihaka and
Gentleman (1996), what evolved into the R Project for Statistical
Computing was originated by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand.
• A key advantage of the R system is that it is free—simply download
and install it, as we will describe shortly, and then use it. R has
eclipsed its commercial cousin S-PLUS, which is essentially defunct.
6/23/2024 Pravat Uprety 8
What Is R?
• R programming is for statistical computing and is supported by the R
Foundation for Statistical Computing. R programming is used by many
academics and researchers for data and statistical analysis, and the
popularity of R has risen over time.
6/23/2024 Pravat Uprety 9
Installation of R and R Studio
Installation of R
We can download R from the link below and double click
download .[Link].
[Link]
6/23/2024 Pravat Uprety 10
R Studio
• RStudio is an interactive development environment (IDE ) for R. Like
R, RStudio is open-source software, freely available on the internet
for Windows, macOS, Linux, and Unix systems.
• To download RStudio, go to
[Link]
and select the installer appropriate to your operating system. The
RStudio Windows and macOS installers are entirely standard, and you
can accept all of the defaults.
6/23/2024 Pravat Uprety 11
Working with R
• R is a case-sensitive, interpreted language. You can enter commands
one at a time at the command prompt (>) or run a set of commands
from a source file. There are a wide variety of data types, including
vectors, matrices, data frames (similar to datasets), and lists
(collections of objects).
• Most functionality is provided through built-in and user-created
functions and the creation and manipulation of objects. An object is
basically anything that can be assigned a value. For R, that is just
about everything (data, functions, graphs, analytic results, and more).
Every object has a class attribute telling R how to handle it.
6/23/2024 Pravat Uprety 12
• All objects are kept in memory during an interactive session. Basic
functions are available by default. Other functions are contained in
packages that can be attached to a current session as needed.
• Statements consist of functions and assignments. R uses the symbol
<- for assignments, rather than the typical = sign.
• For example, the statement
x <- rnorm(5)
creates a vector object named x containing five random deviates
from a standard normal distribution.
6/23/2024 Pravat Uprety 13
R studio Interface
6/23/2024 Pravat Uprety 14
Basic Math
2+2
2+2
20-10
10*3
45/10
6**2
log(10)
Pi
3^3
3^1250
3/0
6/23/2024 Pravat Uprety 15
Some Arithmetic Operator
Operation Operator Description
Addition x+y Y added to x
Subtraction x-y Y subtracted from x
Multiplication x*y X multiplied by y
Division x/y X divided by y
Exponentiation x^y or x**y X raised to the
power y
Square Root Sqrt (x) Computing the
square root of x
6/23/2024 Pravat Uprety 16
Creating Vector
s <- c(5, 7, 9, 11)
s
length (s)
6/23/2024 Pravat Uprety 17
Example of vector operation
Make the following vectors
f1 <- c (10, 12, 14, 20, 25)
f2 <- c (20, 22, 23, 30, 45)
• Questions to work
Create f3 = f1 +5
Create f4 = f1 x 5
Create f5 = f1 +f3
6/23/2024 Pravat Uprety 18
Clearing the Environment
When you want to clear a single variable from the R environment you can use
the “rm()” command followed by the variable you want to remove.
-> rm(variable)
Removing vectors
rm(s)
Listing vectors
ls()
If you want to delete all the variables that are there in the environment what you
can do is you can use the “rm” with an argument “list” is equal to “ls” followed
by a parenthesis.
-> rm(list=ls())
6/23/2024 Pravat Uprety 19
Clearing the console
• The console can be cleared using the shortcut key “ctrl + L“.
6/23/2024 Pravat Uprety 20
Vector
The simplest data structure in R is a vector, a one-dimensional collection of elements of the same
type. An easy way to create a vector is with the c() function, which combines its arguments.
• Vectors are one-dimensional arrays that can hold numerical data, character data, or a logical data.
• The combine function c() is used to form the vector.
a<- c(1,2,5, 3, 6, -2, 4)
b <- c('one', 'two', 'three')
c<- c(TRUE, TRUE, FALSE, TRUE, FALSE)
Here, a is a numeric vector, b is a character vector, and c is a logical vector.
6/23/2024 Pravat Uprety 21
length (a)
sum (a)
min(a)
max(a)
prod(a)
mean(a)
sort(a)
6/23/2024 Pravat Uprety 22
Data Frame
• A data frame is more general than a matrix in that different column
can contain different modes of data (numeric, character, and so on).
It’s similar to the data set you’d typically see in SAS, SPSS, and Stata.
• Data frame is most common data structure you’ll deal with in R.
6/23/2024 Pravat Uprety 23
A data frame is created with the [Link] ()
function.
• mydata <- [Link] (col1, col2, col3,…….)
Example
Hhno <- c (1,2,3,4,5,6)
Age <- c(25, 32, 43, 34, 52, 29)
Salary <- c(18000, 23400, 54321, 34000, 65000, 21000)
Gender <- c('M', 'F', 'F', 'M', 'F', 'M')
Surveydata <-[Link] ( Hhno, Age, Salary, Gender)
Surveydata
6/23/2024 Pravat Uprety 24
mean (Age)
summary (Gender)
summary (Salary)
plot(Age,Salary)
6/23/2024 Pravat Uprety 25
Example
Make the following vectors:
x <- c(2, 2.5, 3, 3.5, 4, 4.5,6, 7, 9, 10)
y <- c (3, 10, 25, 30, 34, 39, 40, 42, 44, 48)
software <- c("R", "SPSS", "Stata")
Make the data frame as follows:
sno <- seq (1:10)
sno
age <- c (15, 18, 20, 25, 30, 34, 34, 39, 40, 45)
income <- c (19500, 20000, 23000, 28000, 34000, 39000, 40000,
42000, 43000, 52000)
employee <-[Link](sno, age, income)
6/23/2024 Pravat Uprety 26
Packages and Library
• R comes with extensive capabilities right out of the box. But some of
its most exciting features are available as optional modules that we
can download and install.
• There are more than 5,500 user-contributed modules called packages
that we can download easily.
6/23/2024 Pravat Uprety 27
• Packages are collections of R functions, data, and
compiled code in a well-defined format.
• The directory where packages are stored on computer
is called library.
• The library() function loads packages into the R
workspace.
• To install a package for the first time, we have to use
[Link](“package name”) command.
• After installing and to use it in an R session, we need
to load the package using library(package name)
command.
6/23/2024 Pravat Uprety 28
Levels of Measurement
and Measurement Scales
EXAMPLES:
Differences between Height, Age, Weekly Food
Ratio Data measurements, true zero
Spending
exists
Differences between Temperature in Fahrenheit,
Interval Data measurements but no true Standardized exam score
zero
Service quality rating,
Ordered Categories (rankings,
Ordinal Data order, or scaling)
Standard & Poor’s bond
rating, Student letter grades
Categories (no ordering or Marital status, Type of car
Nominal Data direction) owned
6/23/2024 Pravat Uprety 29
Criteria Nominal Ordinal Interval Ratio
Identity/Classificatio √ √ √ √
n
Ordering/Ranking X √ √ √
Distance X X √ √
Origin X X X √
Example Working Post Likert Salary
Depart Scale/GPA
Central values Frequency Mode, Mode, Mode, Median,
(Mode) Median Median, AM AM, GM, HM
Mathematical None < < + < + x
Operator
> > - > - ÷
6/23/2024 Pravat Uprety 30