Unit 1 Introduction to R.pptx
Unit 1 Introduction to R.pptx
Statistical Programming in R
(FT-305B)
SPR/FT 305B/JK 1
Business Analytics Specialization sem 3
• Introduction to BA
• Predictive Modelling
• Statistical Programming in R
SPR/FT 305B/JK 2
SPR/FT 305B/JK 3
SPR/FT 305B/JK 4
SPR/FT 305B/JK 5
CONTENT
• What is R?
• Why R?
• R resources
• Installing Base R and R studio
• R Cosmetics
• The four panes in R studio
• Directory , library , packages
SPR/FT 305B/JK 6
What is R?
R is a programming language and environment
specifically designed for statistical computing, data
analysis, and graphical representation. It is widely used
among statisticians, data scientists, and researchers for
various types of data manipulation, analysis, and
visualization tasks.
SPR/FT 305B/JK 7
Why R?
• Open source
• Interactive
• Operates across various platforms (Window,
Mac,Unix, Linux)
• Scripts and objects can be shared across various
platforms
• Large community for support
• Add – ons are easy
• Versatile and poweful- analytics and visualizations
SPR/FT 305B/JK 8
Key Features of R
SPR/FT 305B/JK 9
Typical Applications of R
•Statistical Analysis and Hypothesis Testing: Researchers use R
for conducting tests and drawing inferences from data.
•Big Data Analytics: With packages like sparklyr and dplyr, R can
handle larger datasets and connect with big data tools.
SPR/FT 305B/JK 10
R Resources
1. Installing R- two software packages: Base-R, and RStudio. Base-R is the
basic software which contains the R programming language. RStudio is
software that makes R programming easier. They are totally free and
open source. (comprehensive r archive network)
http://cran.r-project.org/bin/windows/base/
http://www.rstudio.com/products/rstudio/download/
2. Online resources:
http://www.r-bloggers.com/
http://blog.revolutionanalytics.com/
www.stackoverflow.com
3. Books:
R for Data Science by Garrett Grolemund and Hadley Wickham
R for beginners by Sandeep Rakshit, TMH publication
SPR/FT 305B/JK 11
R Console-Cosmetics
SPR/FT 305B/JK 12
R Studio -Cosmetics
SPR/FT 305B/JK 13
INTEGRATED DEVELOPMENT ENVIRONMENT- IDE
• An IDE consolidate the different aspects of writing a computer
program thus enhances productivity
• Single application: editing source code, building executables,
and debugging.
– Editing source code (Syntax highlighting (visual clues- smart
indentation , color coding) & Autocomplete features)
– Build executables ( compiling & executing code)
– Debugging ( debugging tools and hints like code completion)
SPR/FT 305B/JK 14
The four panes in R Studio
SPR/FT 305B/JK 15
Source: Note pad for code
SPR/FT 305B/JK 16
• Write the script
• Edit the script
• Save the script
• Run/ Rerun command to get output
• Line by line execution or selected
section/complete script can be executed
• Key bindings- Ctrl +S, Ctrl +Enter,
Ctrl+shift+Enter, Ctrl + N
SPR/FT 305B/JK 17
Console: R’s Heart
SPR/FT 305B/JK 18
• R evaluates the code here
• Directly code can be typed and executed in
console but code can’t be saved
• General practice is to code in source and send
it to console by run command where it
generates output
SPR/FT 305B/JK 19
Environment/History
SPR/FT 305B/JK 20
• Environment:
Data objects being defined in the current R
session
Data objects- information (observations,
rows)
SPR/FT 305B/JK 21
Files / Plots / Packages / Help
SPR/FT 305B/JK 22
Files :
• The files panel gives you access to the file directory on
your hard drive.
• Feature of the “Files” panel is that you can use it to set
your working directory - once you navigate to a folder
you want to read and save files to, click “More” and then
“Set As Working Directory.”
Plots
• Plot graphs.There are buttons for opening the plot
in a separate window and exporting/ saving the
plot as a pdf or jpeg
SPR/FT 305B/JK 23
Packages :
• R packages installed on your harddrive
• R packages loaded in the current session
(checked ).
• Help - Help menu for R functions. You can
either type the name of a function in the
search window, or use the code to search for a
function with the name
• ?hist
• ? t.test
• Vignettes – Tutorial help to use R package
SPR/FT 305B/JK 24
Help, Demonstration, Examples, Packages and
Libraries
25
To start R, double click on the icon .
Then we get the following Gui (Graphic user interface) window
screen
26
HELP :
27
2. Search for help in Google www.google.com
28
29
…continued
30
4. Sometimes, you want to search by the subject on which we
want help (e.g. data input). In such a case, type
help.search("data input")
31
Clicking over the link give required information
32
4. 'help()' for on-line help,
33
34
5) Other useful functions are find and apropos.
For example
> find("lowess") returns
1 "package:stats"
35
Getting Help in R
7) The apropos returns a character vector giving the names of all
objects in the search list that match your enquiry.
apropos("lm") returns
36
•To see a worked example just type the
function name, e.g., lm
•for linear models:
•example(lm)
41
Libraries in R
R provides many functions and one can also write own.
Functions and datasets are organised into libraries
library(.)
library(spatial)
42
Libraries in R
Examples of libraries that come as a part of base package in R.
43
It is easy to use the help function to discover the contents of
library packages.
46
To install any package,
47
Examples :
•The package rmeta contains the statistical tools for
meta analysis.
•The package Agreement contains statistical tools for
measuring agreement.
install.packages("Agreement"
)
Then we get… 24
49
50
Data types in R
Data Type Example R Code
Character "good", "TRUE", “23.4” name <- "John"
51
Objects in R
SPR/FT 305B/JK 52
Movie Data
boxoffic
movie year genre time rating
e
Whatever
2009 35.0 Comedy 92 PG-13
Works
It Follows 2015 15.0 Horror 97 R
Love and Mercy 2015 15.0 Drama 120 R
Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
^ or ** Exponentiation
SPR/FT 305B/JK 54
Logical Operators in R
SPR/FT 305B/JK 55
Logical Operators in R
Operator Description
& Element-wise Logical AND operator. It returns TRUE if both
elements are TRUE
&& Logical AND operator - Returns TRUE if both statements are TRUE
| Elementwise- Logical OR operator. It returns TRUE if one of the
statement is TRUE
|| Logical OR operator. It returns TRUE if one of the statement is
TRUE.
! Logical NOT - returns FALSE if statement is TRUE
SPR/FT 305B/JK 56
Boolean Values inR
• TRUE (T)
• FALSE (F)
SPR/FT 305B/JK 57
Miscellanous Operators in R
SPR/FT 305B/JK 58