Introduction to R Programming Guide
Introduction to R Programming Guide
to
R Programming
• What is R?
• What can you do in R?
• Installing R & R Studio
Overview •
•
RStudio IDE
Help and Navigation
• Installing Packages
Programming in R
• Basic Operations
• Basic Object Types
• Data Structures
• Control structures
• User-defined Functions
• I/O
• Useful build-in Functions
• Plotting
• Sampling
• Distributions
• Statistical Tests
FES - Predictive Society and Data Analytics Lab 2
Getting Started with R: What is R?
R Language
• A well-developed, simple and effective programming language based on S
• Supports imperative, object-oriented and functional programming paradigm
• A vast collection of libraries available for efficient data manipulation and analysis
R Environment
• Open-source and available for Ubuntu, MacOS and Windows
• Highly customizable, flexible and powerful with convenient integration with python, C++, etc.
• Effective data storage and handling facility for fast data manipulation and analysis
• Publish-quality graphics producible both on-screen and on hardcopy
RStudio installation
Workspace
Source Scripts
Visualization/Help
Console
# Load package
# Installing packages from GitHub
> library(survival)
> [Link](‘devtools’)
>
devtools::install_github(‘tidyverse/ggplot2
’)
8
Getting Started with R
• What is R?
• What can you do in R?
• Installing R & R Studio
Overview •
•
RStudio IDE
Help and Navigation
• Installing Packages
Programming in R
• Basic Operations
• Basic Object Types
• Data Structures
• Control structures
• User-defined Functions
• I/O
• Useful build-in Functions
• Plotting
• Sampling
• Distributions
• Statistical Tests
FES - Predictive Society and Data Analytics Lab 9
Programming in R: Basic Operations
•Assigning values to variables • Mathematical operators
[[3]]
[1] 3
Factor (Kind of a sorter)
[[4]] height <-factor(c(“small”, “medium”, “tall”,
[1] 4 2
“small”, “medium”))
names(b) <- LETTERS[1:4] height
b$C [1] small medium tall small medium
[1] 3 Levels: medium small tall
d
Second
Second
third
FES - Predictive Society and Data Analytics Lab 19
Programming in R: Control Structures
While loop • Other Functions usable for control
1. Ifelse() for vectors
• Returning values
Reading
To load files saved using R save() (i.e., ‘.rda’ or ‘.Rdata’) use load()
Data saved # Load .rda or .Rdata files
> load(“[Link]”)
by R
[Link]() # read a comma seperated file [Link]() # read a file in table format
[Link]() # read a TAB delimited file [Link]() # read a fixed width
From other format table
file types [Link]() # read data interchange format file
file
readLines() # Read individual lines from a
save(a, b,c,d,file="filename")
Saving
e<-c("a","b","c","d") save(list=e,file="filename")
Data in R
save(list=ls(all),file="filename")
[Link](file="filename")
• Vector Functions
• ave(), sort(), dim(), sum(), prod(), sample(), mean(), median(),
union(), unique(), sd(), quantilie(), range(), diff(), min(), max()
• String Functions
• substr(), grep(), sub(), paste(), strsplit(), tolower(), toupper()
FES - Predictive Society and Data Analytics Lab The data taken from 26
[Link]
Programming in R: Plotting Example
FES - Predictive Society and Data Analytics Lab The data taken from 27
[Link]
Programming in R: Plotting Example
• Built-in color options in R
FES - Predictive Society and Data Analytics Lab The data taken from 30
[Link]
Programming in R: Plotting Example
FES - Predictive Society and Data Analytics Lab The data taken from 31
[Link]
Programming in R: Plotting Example
FES - Predictive Society and Data Analytics Lab The data taken from 32
[Link]
Programming in R: Plotting Example
FES - Predictive Society and Data Analytics Lab The data taken from 33
[Link]
Getting Started with R
• What is R?
• What can you do in R?
• Installing R & R Studio
Overview •
•
RStudio IDE
Help and Navigation
• Installing Packages
Programming in R
• Basic Operations
• Basic Object Types
• Data Structures
• Control structures
• User-defined Functions
• I/O
• Useful build-in Functions
• Plotting
• Sampling
• Distributions
• Statistical Tests
FES - Predictive Society and Data Analytics Lab 34
Probability and Statistics in R: Sampling
• Sampling from normal distribution
• Sampling from distribution
example
# Sampling without replacement
R <- 10
D <- rnorm(50) # Random normal
• We can use the following distribution
sampled <- sample(D, R) # By default replace=F
functions to generate a random
sample from common # Sampling with replacement
distributions such as normal, chi- R <- 10
D <- rnorm(50, 5, 2) # Random normal
squared, poisson, etc
distribution with mean = 5 and sd = 2
sampled <- sample(D, R, replace=T)
sampled
• Use “?rnorm” for details
FES - Predictive Society and Data Analytics Lab 35
Probability and Statistics in R: Distributions
• Each Distribution has functions with
p, q, d and r for the following
• p for "probability", the cumulative distribution
function (c. d. f.)
• q for "quantile", the inverse c. d. f.
• d for "density", the density function (p. f. or
p. d. f.)
• r for "random", a random variable having the
specified distribution