SlideShare una empresa de Scribd logo
1 de 53
The R Language
Dr. Smruti R. Sarangi and Ms. Hameedah Sultan
Computer Science and Engineering
IIT Delhi
1
Overview of R
Language for statistical computing and data analysis
Freely available under GPL v2
Extensive library support
Programming paradigms
procedural
functional
object-oriented
General matrix computation (similar to Matlab)
2
Running R
Command Line
Just type R
 The R command prompt comes up
> .....
With a GUI
R Studio
R Commander
3
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
4
Normal Variables
 We can use <- as the assignment operator in R
 > x <- 4
(set x to 4)
For printing the value of x
> x
[1] 4
OR, > print(x)
[1] 4
5
A Numeric Vector
 Simplest data structure
Numeric vector
> v <- c(1,2,3)
 <- is the assignment operator
 c is the list concatenation operator
To print the value, v
Type : > v
Output: [1] 1 2 3
6
A vector is a full fledged variable
 Let us do the following:
 > 1/v
[1] 1.0000000 0.5000000 0.3333333
 > v + 2
[1] 3 4 5
We can treat a vector as a regular variable
For example, we can have:
> v1 <- v / 2
> v1
[1] 0.5 1.0 1.5
7
Creating a vector with vectors
> v <- c (1,2,3)
> v
[1] 1 2 3
> vnew <- c (v,0,v)
> vnew
[1] 1 2 3 0 1 2 3
The c operator concatenates all the vectors
8
Functions on Vectors and Complex
Numbers
 If v is a vector
 Here, are a few of the functions that take vectors as
inputs:
mean(v), max(v), sqrt(v), length(v), sum(v), prod(v), sort (v)
(in ascending order)
> x <- 1 + 1i
> y <- 1i
> x * y
[1] -1+1i
9
Generating Vectors
 Suppose we want a vector of the form:
(1,2,3,... 100)
 We do not have to generate it manually.
 We can use the following commands:
> v <- 1:100
OR
> v <- seq(1,100)
 seq takes an additional argument, which is the difference between
consecutive numbers:
 seq (1,100,10) gives (1,11,21,31 ... , 91)
 rep (2,5) generates a vector (2, 2, 2, 2, 2)
10
Boolean Variables and Vectors
 R recognizes the constants: TRUE, FALSE
 TRUE corresponds to 1
 FALSE corresponds to 0
We can define a vector of the form:
 v <- c (TRUE, FALSE, TRUE)
 We can also define a logical vector
 Can be created with logical operators: <, <=, >=, ==, !=,
& and I
> v <- 1:9 > 5
> v
[1] FALSE FALSE FALSE FALSE FALSE TRUE
TRUE TRUE TRUE
11
String Vectors
 Similarly, we can have a vector of strings
 > vec <- c (“f1”, “f2”, “f3”)
> vec
[1] "f1" "f2" "f3“
 The paste function can be used to create a vector of strings
paste(1:3, 3:5,sep="*")
[1] "1*3" "2*4" "3*5"
It takes two vectors of the same length, and an optional
argument, sep. The ith element of the result string, contains the ith
elements of both the arguments, separated by the string specified
by sep.
12
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
13
Factors
Consider the following problem:
We have a vector of the type of the Nationality of students,
and a vector of their marks in a given subject.
 AIM: Find the average scores per nationality.
Factor Definition: A vector used to specify
a grouping (classification) of objects
in other vectors.
14
Graphical View of the Problem
Indian
Chinese
Indian
Chinese
Indian
Russian
6
8
7
9
8
10
Nationality Marks
Indian
Chinese
Russian
Factor
15
Code
The levels of a factor indicate the categories
> nationalities <- c ("Indian", "Chinese", "Indian", "Chinese",
"Indian", "Russian") # create a factor
> marks <- c (6, 8, 7, 9, 8, 10)
# character starts a
comment
> fac <- factor(nationalities)
> fac
[1] Indian Chinese Indian Chinese Indian Russian
Levels: Chinese Indian Russian
16
Code - II
 Now let us apply the factor to the marks vector
> results <- tapply (marks, fac, mean)
List of marks
Works on each element
of the list
factor
compute the mean in
each category
17
Time for the results
 Let us now apply the sum function
> tapply (marks, fac, sum)
Chinese Indian Russian
17 21 10
> results
Chinese Indian Russian
8.5 7.0 10.0
18
levels and table
 Let us assume that the factor is fac.
 fac is
[1] Indian Chinese Indian Chinese Indian Russian
Levels: Chinese Indian Russian
 levels returns a vector containing all the unique
labels
 table returns a special kind of array that contains the
counts of entries for each label
> levels (fac)
[1] "Chinese" "Indian" "Russian"
> table (fac)
fac
Chinese Indian Russian
2 3 1
19
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
20
Arrays and Matrices
 Generic array function
 Creates an array. Takes two arguments:
 data_vector  vector of values
 dimension_vector
 Example:
> array (1:10, c(2,5))
[,1 [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
The numbers are laid out in column major order.
Count from 1, Not 0
21
Other ways to make arrays
 Take a vector, and assign it dimensions
 > v <- c (1,2,3,4)
> dim(v) <- c(2,2)
> v
[,1] [,2]
[1,] 1 3
[2,] 2 4
22
Arrays are Created in Column Major Order
> v <- 1:8
> dim(v) <- c(2,2,2)
> v
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
> v[2,1,2]
[1] 6
Start from the last index
Array elements are accessed by
specifying their index (within
square brackets)
23
The matrix command
 A matrix is a 2-D array
There is a fast method of creating a matrix
 Use the matrix (data, dim1, dim2) command
Example:
> matrix(1:4, 2, 2)
[,1] [,2]
[1,] 1 3
[2,] 2 4
24
cbind and rbind
mat1 mat2
cbind
mat1 mat2
mat1 mat2
rbind
mat1
mat2
25
Problem: set the diagonal elements of a
matrix to 0
> mat <- matrix(1:16,4,4)
> mat
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> indices <- cbind (1:4, 1:4)
> mat[indices] <- 0
> mat
[,1] [,2] [,3] [,4]
[1,] 0 5 9 13
[2,] 2 0 10 14
[3,] 3 7 0 15
[4,] 4 8 12 0
26
Recycling Rule
The smaller structure is replicated to match the
length of the longer structure
Note that the size of the longer structure has to be a
multiple of the size of the smaller structure.
> cbind (1:4, 1:8)
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 1 5
[6,] 2 6
[7,] 3 7
[8,] 4 8
27
Matrix Operations
 A * B is a normal element-by-element product
A %*% B is a matrix product
 Equation solution:
 solve (A, b) (for equations of the form Ax = b)
 solve (A) returns the inverse of the matrix
> A <- matrix (1:4, 2, 2)
> b <- 5:6
> solve (A,b)
[1] -1 2
> solve(A) %*% b
[,1]
[1,] -1
[2,] 2
Solve an equation of the form: Ax
= b
A-1 * b = x
28
Additional Features
Feature Function
Eigen Values eigen
Singular Value Decomposition svd
Least Squares Fitting lsfit
QR decomposition qr
29
 nrow (mat)  Number of rows in the matrix
 ncol (mat)  Number of columns in the matrix
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
30
Lists and Data Frames
 A list is a heterogeneous data structure
 It can contain data belonging to all kinds of types
 Example:
 > lst <- list (“one”, 1, TRUE)
 Elements can be lists, arrays, factors, and normal variables
 The components are always numbered
 They are accessed as follows: lst[[1]], lst[[2]], lst[[3]]
 [[ ... ]] is the operator for accessing an element in a list
31
Named Components
 Lists can also have named components
 lst <- list(name=“Sofia”, age=29, marks=33.7)
 The three components are: lst$name, lst$age, lst$marks
 We can also use
 lst [[“name”]], lst[[“age”]], lst [[“marks”]]
32
Data Frames
 It is a table in R
Data Framerows
columns
> entries <- c(“cars”, “trucks”, “bikes”)
> price <- c (8, 10, 5)
> num <- c (1, 2, 3)
> df <- data.frame(entries, price, num)
> df
entries price num
1 cars 8 1
2 trucks 10 2
3 bikes 5 3
33
Accessing an Element
 Can be accessed as a regular array, or as a list
> df[1,2]
[1] 8
> df[2,]
entries price num
2 trucks 10 2
> df$price
[1] 8 10 5
 Summary shows a summary of each variable in the data frame
> summary(df)
entries price num
bikes :1 Min. : 5.000 Min. :1.0
cars :1 1st Qu.: 6.500 1st Qu.:1.5
trucks:1 Median : 8.000 Median :2.0
Mean : 7.667 Mean :2.0
3rd Qu.: 9.000 3rd Qu.:2.5
Max. :10.000 Max. :3.0
34
Row names, i.e.
character values
Feature Function
Show first 6 rows of df head(df)
List objects ls()
Remove variables x & y
from data frame
rm(x,y)
Sort df on variable x [order(df$x),]
Operations on Data Frames
 A data frame can be sorted on the values of a variable, filtered
using values of a variable, and grouped by a variable.
 Eg. Filter rows where entries = “cars”
> df[df$entries == "cars",]
entries price num
1 cars 8 1
 Group by entries
> aggregate(df,by = list(entries), mean)
Group.1 entries price num
1 bikes NA 5 3
2 cars NA 8 1
3 trucks NA 10 2
35
Reading Data from Files
 Reads in a data frame from a file
 Steps:
 Store the data frame in a file
 Read it in
 > df <- read.table (“<filename>”)
 Access the data frame
36
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
37
Grouping, Loops, Conditional Execution
 R does have support for regular if statements, while
loops, and other conditionals
 if statement
 if (condition) statement 1 else statement 2. Use {} for
creating grouped statements
 The condition should evaluate to a single variable (not a
vector)
 Example:
> x <- 3
> if (x > 0) x <- x+ 3 else x <- x + 6
> x
[1] 6
38
For loop
 for (var in expr1) {
....
....
}
Example: > for (v in 1:10) print (v)
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
39
While loop
> while (x[i] < 10) {
+ print (x[i])
+ i <- i + 1
+ }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
Use the break statement to exit a
loop
40
Writing one’s own functions
 A function takes a list of arguments within ( ... )
 To return a value, just print the expression (without
assignment statements)
 Function calling convention  similar to C
> cube <- function (x) {
+ x * x * x
+ }
> cube(4)
[1] 64
41
Applying a Function
 Apply the cube function to a vector
 Applies the function to each and every argument
sapply returns a list
> lapply (1:2,cube)
[[1]]
[1] 1
[[2]]
[1] 8
> sapply (1:3, cube)
[1] 1 8 27
42
Named arguments
 Possible to specify default values in the function
declaration
 If a variable is not specified, the default value is used
 We can also specify the values of the variables by the name of
the argument (last line)
> fun <- function (x=4, y=3) { x - y }
> fun()
[1] 1
> fun (4,3)
[1] 1
> fun (y=4, x=3)
[1] -1
43
Scoping in R
 Scope of variables in R
 Function arguments (valid only inside the function)
 Local variables (valid only inside the function)
 Global variables (balance)
> deposit <- function (amt) balance + amt
> withdraw <- function (amt) balance - amt
> balance <- withdraw(10)
> balance <- deposit (20)
> balance
[1] 110
44
Functional Programming: Closures
 A function with pre-specified data is called a closure
 exponent returns a function power (with n = 2)
45
> exponent <- function (n) {
+ power <- function (x) {
+ x ** n
+ }
+ }
> square <- exponent(2)
> square(4)
[1] 16
Example: Numerical Integration
> composite <- function(f, a, b, n = 10,
rule) {
area <- 0
+ points <- seq(a, b, length = n + 1)
+
+ area <- 0
+ for (i in seq_len(n)) {
+ area <- area + rule(f, points[i],
points[i + 1])
+ }
+
+ area
+ }
> midpoint <- function(f, a, b) {
+ (b - a) * f((a + b) / 2)
+ }
> composite(sin, 0, pi, n = 1000, rule =
midpoint)
[1] 2.00000
Function for
numerical
integration
http://adv-r.had.co.nz/Functional-programming.htmlsource
Midpoint rule
0
𝜋
sin 𝑥 𝑑𝑥
function passed as
an argument
46
Outline
 Variables and Vectors
 Factors
 Arrays and Matrices
 Data Frames
 Functions and Conditionals
 Graphical Procedures
47
Plotting a Function
 A basic 2D plot:
vec1 <-cube(seq(1,100,10))
vec2 <-cube(seq(5,100,10))
plot(vec1, type="o", col="blue“, ylim=c(0,3e5))
title(main=“Plot of Cubes", col.main="red")
 To add a line to the same plot:
lines(vec2, type=“o", lty = 2, pch = 22, col=“red“)
 To add a legend:
legend(1, max(vec1), c(“vec1",“vec2"), cex=0.8, col=c("blue","red"),
pch=21:22, lty=1:2)
48
Plot type
(overplotted)
Line type:
dashed
Marker type:
square
Plotting: Linear Regression
library("MASS")
data(cats) # load data
plot(cats$Bwt, cats$Hwt) # scatter plot of cats body weight vs heart rate
M <- lm(formula = cats$Hwt ~ cats$Bwt, data=cats) # fit a linear model
regmodel <- predict(M) # predict values using this model
plot(cats$Bwt, cats$Hwt, pch = 16, cex = 1.3, col = "blue", main = "Heart
rate plotted against body weight of cats", xlab = "Body weight", ylab =
"Heart rate") # scatter plot
abline(M) # plot the regression line
49
Creating 3-D plots
Packages plot3D, ggplot2 contain useful 3D plotting
options
 plot3d, scatter3d, surf3d, persp3d are some of
the commonly used plots.
plot3d is from package rgl.
It allows creating interactive 3D plots that can be rotated
using the mouse.
plot3d(x, y, z, col="red", size=3)
50
Creating 3-D plots: surf3D
Surf3d (package: plot3D) allows us to create surface
plots like the one shown below:
51
#source: http://blog.revolutionanalytics.com/2014/02/3d-
plots-in-r.html
library ('ggplot2')
library(plot3D)
par(mar = c(2, 2, 2, 2))
par(mfrow = c(1, 1))
R <- 3; r <- 2
x <- seq(0, 2*pi,length.out=50)
y <- seq(0, pi,length.out=50)
M <- mesh(x, y)
alpha <- M$x; beta <- M$y
surf3D(x = (R + r*cos(alpha)) * cos(beta),
y = (R + r*cos(alpha)) * sin(beta),
z = r * sin(alpha),
colkey=FALSE,
bty="b2",
main="Half of a Torus")
Creating 3-D plots: persp3d
persp3d(package: plot3D) allows us to create surface
plots like the one shown below:
52
xdim <- 16
newmap <- array(0,dim=c(xdim,xdim))
newmap <- rnorm(256,1,.2)
jet.colors <- colorRampPalette( c("yellow", "red") )
pal <- jet.colors(100)
col.ind <- cut(newmap,100) # colour indices of each point
persp3d(seq(1:xdim),seq(1:xdim),newmap,shade=TRUE,
type="wire", col=pal[col.ind],xlab="",ylab="",zlab="",
cex.axis=1.5,xtics="",aspect=2,zlim=c(0,5))
53

Más contenido relacionado

La actualidad más candente

R programming presentation
R programming presentationR programming presentation
R programming presentationAkshat Sharma
 
R Programming Language
R Programming LanguageR Programming Language
R Programming LanguageNareshKarela1
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualizationDr. Hamdan Al-Sabri
 
Data analytics using R programming
Data analytics using R programmingData analytics using R programming
Data analytics using R programmingUmang Singh
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data framekrishna singh
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With REdureka!
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining Sulman Ahmed
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentationRatul Hasan
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programmingVictor Ordu
 

La actualidad más candente (20)

R programming
R programmingR programming
R programming
 
R programming presentation
R programming presentationR programming presentation
R programming presentation
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
R data types
R   data typesR   data types
R data types
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
Statistics for data science
Statistics for data science Statistics for data science
Statistics for data science
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualization
 
R programming
R programmingR programming
R programming
 
Data analytics using R programming
Data analytics using R programmingData analytics using R programming
Data analytics using R programming
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
3. R- list and data frame
3. R- list and data frame3. R- list and data frame
3. R- list and data frame
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining
 
Clustering in Data Mining
Clustering in Data MiningClustering in Data Mining
Clustering in Data Mining
 
01 Data Mining: Concepts and Techniques, 2nd ed.
01 Data Mining: Concepts and Techniques, 2nd ed.01 Data Mining: Concepts and Techniques, 2nd ed.
01 Data Mining: Concepts and Techniques, 2nd ed.
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 

Destacado

R programming language
R programming languageR programming language
R programming languageKeerti Verma
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & AdvancedSohom Ghosh
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)Dataspora
 
How to get started with R programming
How to get started with R programmingHow to get started with R programming
How to get started with R programmingRamon Salazar
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of RWinston Chen
 
Learning R and Teaching R
Learning R and Teaching RLearning R and Teaching R
Learning R and Teaching RAjay Ohri
 
Formulation Lpp
Formulation  LppFormulation  Lpp
Formulation LppSachin MK
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-iDr. Awase Khirni Syed
 
R language Project report
R language Project reportR language Project report
R language Project reportTianyue Wang
 
Definition of linear programming problem model decision variable, objective ...
Definition of linear programming problem model decision variable, objective ...Definition of linear programming problem model decision variable, objective ...
Definition of linear programming problem model decision variable, objective ...Sunny Mervyne Baa
 
Applications of linear programming
Applications of linear programmingApplications of linear programming
Applications of linear programmingZenblade 93
 
Linear Programming 1
Linear Programming 1Linear Programming 1
Linear Programming 1irsa javed
 
Linear programming - Model formulation, Graphical Method
Linear programming  - Model formulation, Graphical MethodLinear programming  - Model formulation, Graphical Method
Linear programming - Model formulation, Graphical MethodJoseph Konnully
 

Destacado (20)

R programming language
R programming languageR programming language
R programming language
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & Advanced
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
 
How to get started with R programming
How to get started with R programmingHow to get started with R programming
How to get started with R programming
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Linear programming
Linear programmingLinear programming
Linear programming
 
Mughal empire
Mughal empireMughal empire
Mughal empire
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of R
 
Learning R and Teaching R
Learning R and Teaching RLearning R and Teaching R
Learning R and Teaching R
 
Formulation Lpp
Formulation  LppFormulation  Lpp
Formulation Lpp
 
Higher Education and the Socio-Economic Development of Indian Minorities
Higher Education and the Socio-Economic Development of Indian MinoritiesHigher Education and the Socio-Economic Development of Indian Minorities
Higher Education and the Socio-Economic Development of Indian Minorities
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
R language Project report
R language Project reportR language Project report
R language Project report
 
Definition of linear programming problem model decision variable, objective ...
Definition of linear programming problem model decision variable, objective ...Definition of linear programming problem model decision variable, objective ...
Definition of linear programming problem model decision variable, objective ...
 
LSESU a Taste of R Language Workshop
LSESU a Taste of R Language WorkshopLSESU a Taste of R Language Workshop
LSESU a Taste of R Language Workshop
 
Applications of linear programming
Applications of linear programmingApplications of linear programming
Applications of linear programming
 
Linear Programming 1
Linear Programming 1Linear Programming 1
Linear Programming 1
 
Linear programming ppt
Linear programming pptLinear programming ppt
Linear programming ppt
 
Linear programming - Model formulation, Graphical Method
Linear programming  - Model formulation, Graphical MethodLinear programming  - Model formulation, Graphical Method
Linear programming - Model formulation, Graphical Method
 
M02 un10 p02
M02 un10 p02M02 un10 p02
M02 un10 p02
 

Similar a Programming in R

Similar a Programming in R (20)

R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
R Basics
R BasicsR Basics
R Basics
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
R교육1
R교육1R교육1
R교육1
 
R programming
R programmingR programming
R programming
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
R Programming Homework Help
R Programming Homework HelpR Programming Homework Help
R Programming Homework Help
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
R basics
R basicsR basics
R basics
 
R language introduction
R language introductionR language introduction
R language introduction
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
bobok
bobokbobok
bobok
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
8. Vectors data frames
8. Vectors data frames8. Vectors data frames
8. Vectors data frames
 

Último

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 

Último (20)

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

Programming in R

  • 1. The R Language Dr. Smruti R. Sarangi and Ms. Hameedah Sultan Computer Science and Engineering IIT Delhi 1
  • 2. Overview of R Language for statistical computing and data analysis Freely available under GPL v2 Extensive library support Programming paradigms procedural functional object-oriented General matrix computation (similar to Matlab) 2
  • 3. Running R Command Line Just type R  The R command prompt comes up > ..... With a GUI R Studio R Commander 3
  • 4. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 4
  • 5. Normal Variables  We can use <- as the assignment operator in R  > x <- 4 (set x to 4) For printing the value of x > x [1] 4 OR, > print(x) [1] 4 5
  • 6. A Numeric Vector  Simplest data structure Numeric vector > v <- c(1,2,3)  <- is the assignment operator  c is the list concatenation operator To print the value, v Type : > v Output: [1] 1 2 3 6
  • 7. A vector is a full fledged variable  Let us do the following:  > 1/v [1] 1.0000000 0.5000000 0.3333333  > v + 2 [1] 3 4 5 We can treat a vector as a regular variable For example, we can have: > v1 <- v / 2 > v1 [1] 0.5 1.0 1.5 7
  • 8. Creating a vector with vectors > v <- c (1,2,3) > v [1] 1 2 3 > vnew <- c (v,0,v) > vnew [1] 1 2 3 0 1 2 3 The c operator concatenates all the vectors 8
  • 9. Functions on Vectors and Complex Numbers  If v is a vector  Here, are a few of the functions that take vectors as inputs: mean(v), max(v), sqrt(v), length(v), sum(v), prod(v), sort (v) (in ascending order) > x <- 1 + 1i > y <- 1i > x * y [1] -1+1i 9
  • 10. Generating Vectors  Suppose we want a vector of the form: (1,2,3,... 100)  We do not have to generate it manually.  We can use the following commands: > v <- 1:100 OR > v <- seq(1,100)  seq takes an additional argument, which is the difference between consecutive numbers:  seq (1,100,10) gives (1,11,21,31 ... , 91)  rep (2,5) generates a vector (2, 2, 2, 2, 2) 10
  • 11. Boolean Variables and Vectors  R recognizes the constants: TRUE, FALSE  TRUE corresponds to 1  FALSE corresponds to 0 We can define a vector of the form:  v <- c (TRUE, FALSE, TRUE)  We can also define a logical vector  Can be created with logical operators: <, <=, >=, ==, !=, & and I > v <- 1:9 > 5 > v [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE 11
  • 12. String Vectors  Similarly, we can have a vector of strings  > vec <- c (“f1”, “f2”, “f3”) > vec [1] "f1" "f2" "f3“  The paste function can be used to create a vector of strings paste(1:3, 3:5,sep="*") [1] "1*3" "2*4" "3*5" It takes two vectors of the same length, and an optional argument, sep. The ith element of the result string, contains the ith elements of both the arguments, separated by the string specified by sep. 12
  • 13. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 13
  • 14. Factors Consider the following problem: We have a vector of the type of the Nationality of students, and a vector of their marks in a given subject.  AIM: Find the average scores per nationality. Factor Definition: A vector used to specify a grouping (classification) of objects in other vectors. 14
  • 15. Graphical View of the Problem Indian Chinese Indian Chinese Indian Russian 6 8 7 9 8 10 Nationality Marks Indian Chinese Russian Factor 15
  • 16. Code The levels of a factor indicate the categories > nationalities <- c ("Indian", "Chinese", "Indian", "Chinese", "Indian", "Russian") # create a factor > marks <- c (6, 8, 7, 9, 8, 10) # character starts a comment > fac <- factor(nationalities) > fac [1] Indian Chinese Indian Chinese Indian Russian Levels: Chinese Indian Russian 16
  • 17. Code - II  Now let us apply the factor to the marks vector > results <- tapply (marks, fac, mean) List of marks Works on each element of the list factor compute the mean in each category 17
  • 18. Time for the results  Let us now apply the sum function > tapply (marks, fac, sum) Chinese Indian Russian 17 21 10 > results Chinese Indian Russian 8.5 7.0 10.0 18
  • 19. levels and table  Let us assume that the factor is fac.  fac is [1] Indian Chinese Indian Chinese Indian Russian Levels: Chinese Indian Russian  levels returns a vector containing all the unique labels  table returns a special kind of array that contains the counts of entries for each label > levels (fac) [1] "Chinese" "Indian" "Russian" > table (fac) fac Chinese Indian Russian 2 3 1 19
  • 20. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 20
  • 21. Arrays and Matrices  Generic array function  Creates an array. Takes two arguments:  data_vector  vector of values  dimension_vector  Example: > array (1:10, c(2,5)) [,1 [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 The numbers are laid out in column major order. Count from 1, Not 0 21
  • 22. Other ways to make arrays  Take a vector, and assign it dimensions  > v <- c (1,2,3,4) > dim(v) <- c(2,2) > v [,1] [,2] [1,] 1 3 [2,] 2 4 22
  • 23. Arrays are Created in Column Major Order > v <- 1:8 > dim(v) <- c(2,2,2) > v , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 > v[2,1,2] [1] 6 Start from the last index Array elements are accessed by specifying their index (within square brackets) 23
  • 24. The matrix command  A matrix is a 2-D array There is a fast method of creating a matrix  Use the matrix (data, dim1, dim2) command Example: > matrix(1:4, 2, 2) [,1] [,2] [1,] 1 3 [2,] 2 4 24
  • 25. cbind and rbind mat1 mat2 cbind mat1 mat2 mat1 mat2 rbind mat1 mat2 25
  • 26. Problem: set the diagonal elements of a matrix to 0 > mat <- matrix(1:16,4,4) > mat [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > indices <- cbind (1:4, 1:4) > mat[indices] <- 0 > mat [,1] [,2] [,3] [,4] [1,] 0 5 9 13 [2,] 2 0 10 14 [3,] 3 7 0 15 [4,] 4 8 12 0 26
  • 27. Recycling Rule The smaller structure is replicated to match the length of the longer structure Note that the size of the longer structure has to be a multiple of the size of the smaller structure. > cbind (1:4, 1:8) [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3 [4,] 4 4 [5,] 1 5 [6,] 2 6 [7,] 3 7 [8,] 4 8 27
  • 28. Matrix Operations  A * B is a normal element-by-element product A %*% B is a matrix product  Equation solution:  solve (A, b) (for equations of the form Ax = b)  solve (A) returns the inverse of the matrix > A <- matrix (1:4, 2, 2) > b <- 5:6 > solve (A,b) [1] -1 2 > solve(A) %*% b [,1] [1,] -1 [2,] 2 Solve an equation of the form: Ax = b A-1 * b = x 28
  • 29. Additional Features Feature Function Eigen Values eigen Singular Value Decomposition svd Least Squares Fitting lsfit QR decomposition qr 29  nrow (mat)  Number of rows in the matrix  ncol (mat)  Number of columns in the matrix
  • 30. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 30
  • 31. Lists and Data Frames  A list is a heterogeneous data structure  It can contain data belonging to all kinds of types  Example:  > lst <- list (“one”, 1, TRUE)  Elements can be lists, arrays, factors, and normal variables  The components are always numbered  They are accessed as follows: lst[[1]], lst[[2]], lst[[3]]  [[ ... ]] is the operator for accessing an element in a list 31
  • 32. Named Components  Lists can also have named components  lst <- list(name=“Sofia”, age=29, marks=33.7)  The three components are: lst$name, lst$age, lst$marks  We can also use  lst [[“name”]], lst[[“age”]], lst [[“marks”]] 32
  • 33. Data Frames  It is a table in R Data Framerows columns > entries <- c(“cars”, “trucks”, “bikes”) > price <- c (8, 10, 5) > num <- c (1, 2, 3) > df <- data.frame(entries, price, num) > df entries price num 1 cars 8 1 2 trucks 10 2 3 bikes 5 3 33
  • 34. Accessing an Element  Can be accessed as a regular array, or as a list > df[1,2] [1] 8 > df[2,] entries price num 2 trucks 10 2 > df$price [1] 8 10 5  Summary shows a summary of each variable in the data frame > summary(df) entries price num bikes :1 Min. : 5.000 Min. :1.0 cars :1 1st Qu.: 6.500 1st Qu.:1.5 trucks:1 Median : 8.000 Median :2.0 Mean : 7.667 Mean :2.0 3rd Qu.: 9.000 3rd Qu.:2.5 Max. :10.000 Max. :3.0 34 Row names, i.e. character values Feature Function Show first 6 rows of df head(df) List objects ls() Remove variables x & y from data frame rm(x,y) Sort df on variable x [order(df$x),]
  • 35. Operations on Data Frames  A data frame can be sorted on the values of a variable, filtered using values of a variable, and grouped by a variable.  Eg. Filter rows where entries = “cars” > df[df$entries == "cars",] entries price num 1 cars 8 1  Group by entries > aggregate(df,by = list(entries), mean) Group.1 entries price num 1 bikes NA 5 3 2 cars NA 8 1 3 trucks NA 10 2 35
  • 36. Reading Data from Files  Reads in a data frame from a file  Steps:  Store the data frame in a file  Read it in  > df <- read.table (“<filename>”)  Access the data frame 36
  • 37. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 37
  • 38. Grouping, Loops, Conditional Execution  R does have support for regular if statements, while loops, and other conditionals  if statement  if (condition) statement 1 else statement 2. Use {} for creating grouped statements  The condition should evaluate to a single variable (not a vector)  Example: > x <- 3 > if (x > 0) x <- x+ 3 else x <- x + 6 > x [1] 6 38
  • 39. For loop  for (var in expr1) { .... .... } Example: > for (v in 1:10) print (v) [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 39
  • 40. While loop > while (x[i] < 10) { + print (x[i]) + i <- i + 1 + } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 Use the break statement to exit a loop 40
  • 41. Writing one’s own functions  A function takes a list of arguments within ( ... )  To return a value, just print the expression (without assignment statements)  Function calling convention  similar to C > cube <- function (x) { + x * x * x + } > cube(4) [1] 64 41
  • 42. Applying a Function  Apply the cube function to a vector  Applies the function to each and every argument sapply returns a list > lapply (1:2,cube) [[1]] [1] 1 [[2]] [1] 8 > sapply (1:3, cube) [1] 1 8 27 42
  • 43. Named arguments  Possible to specify default values in the function declaration  If a variable is not specified, the default value is used  We can also specify the values of the variables by the name of the argument (last line) > fun <- function (x=4, y=3) { x - y } > fun() [1] 1 > fun (4,3) [1] 1 > fun (y=4, x=3) [1] -1 43
  • 44. Scoping in R  Scope of variables in R  Function arguments (valid only inside the function)  Local variables (valid only inside the function)  Global variables (balance) > deposit <- function (amt) balance + amt > withdraw <- function (amt) balance - amt > balance <- withdraw(10) > balance <- deposit (20) > balance [1] 110 44
  • 45. Functional Programming: Closures  A function with pre-specified data is called a closure  exponent returns a function power (with n = 2) 45 > exponent <- function (n) { + power <- function (x) { + x ** n + } + } > square <- exponent(2) > square(4) [1] 16
  • 46. Example: Numerical Integration > composite <- function(f, a, b, n = 10, rule) { area <- 0 + points <- seq(a, b, length = n + 1) + + area <- 0 + for (i in seq_len(n)) { + area <- area + rule(f, points[i], points[i + 1]) + } + + area + } > midpoint <- function(f, a, b) { + (b - a) * f((a + b) / 2) + } > composite(sin, 0, pi, n = 1000, rule = midpoint) [1] 2.00000 Function for numerical integration http://adv-r.had.co.nz/Functional-programming.htmlsource Midpoint rule 0 𝜋 sin 𝑥 𝑑𝑥 function passed as an argument 46
  • 47. Outline  Variables and Vectors  Factors  Arrays and Matrices  Data Frames  Functions and Conditionals  Graphical Procedures 47
  • 48. Plotting a Function  A basic 2D plot: vec1 <-cube(seq(1,100,10)) vec2 <-cube(seq(5,100,10)) plot(vec1, type="o", col="blue“, ylim=c(0,3e5)) title(main=“Plot of Cubes", col.main="red")  To add a line to the same plot: lines(vec2, type=“o", lty = 2, pch = 22, col=“red“)  To add a legend: legend(1, max(vec1), c(“vec1",“vec2"), cex=0.8, col=c("blue","red"), pch=21:22, lty=1:2) 48 Plot type (overplotted) Line type: dashed Marker type: square
  • 49. Plotting: Linear Regression library("MASS") data(cats) # load data plot(cats$Bwt, cats$Hwt) # scatter plot of cats body weight vs heart rate M <- lm(formula = cats$Hwt ~ cats$Bwt, data=cats) # fit a linear model regmodel <- predict(M) # predict values using this model plot(cats$Bwt, cats$Hwt, pch = 16, cex = 1.3, col = "blue", main = "Heart rate plotted against body weight of cats", xlab = "Body weight", ylab = "Heart rate") # scatter plot abline(M) # plot the regression line 49
  • 50. Creating 3-D plots Packages plot3D, ggplot2 contain useful 3D plotting options  plot3d, scatter3d, surf3d, persp3d are some of the commonly used plots. plot3d is from package rgl. It allows creating interactive 3D plots that can be rotated using the mouse. plot3d(x, y, z, col="red", size=3) 50
  • 51. Creating 3-D plots: surf3D Surf3d (package: plot3D) allows us to create surface plots like the one shown below: 51 #source: http://blog.revolutionanalytics.com/2014/02/3d- plots-in-r.html library ('ggplot2') library(plot3D) par(mar = c(2, 2, 2, 2)) par(mfrow = c(1, 1)) R <- 3; r <- 2 x <- seq(0, 2*pi,length.out=50) y <- seq(0, pi,length.out=50) M <- mesh(x, y) alpha <- M$x; beta <- M$y surf3D(x = (R + r*cos(alpha)) * cos(beta), y = (R + r*cos(alpha)) * sin(beta), z = r * sin(alpha), colkey=FALSE, bty="b2", main="Half of a Torus")
  • 52. Creating 3-D plots: persp3d persp3d(package: plot3D) allows us to create surface plots like the one shown below: 52 xdim <- 16 newmap <- array(0,dim=c(xdim,xdim)) newmap <- rnorm(256,1,.2) jet.colors <- colorRampPalette( c("yellow", "red") ) pal <- jet.colors(100) col.ind <- cut(newmap,100) # colour indices of each point persp3d(seq(1:xdim),seq(1:xdim),newmap,shade=TRUE, type="wire", col=pal[col.ind],xlab="",ylab="",zlab="", cex.axis=1.5,xtics="",aspect=2,zlim=c(0,5))
  • 53. 53