R Pres
R Pres
1
Dr. Smruti R. Sarangi and Ms. Hameedah Sultan
Computer Science and Engineering
IIT Delhi
2 Overview of R
Command Line
Just type R
The R command prompt comes up
> .....
With a GUI
R Studio
R Commander
4 Outline
Indian 6
Chinese 8
Indian
Indian 7
Chinese
Chinese 9
Russian
Indian 8
Factor
Russian 10
Nationality Marks
# character starts
16 Code a comment
List of marks
18 Time for the results
> results
Chinese Indian Russian
8.5 7.0 10.0
> v[2,1,2]
[1] 6
24 The matrix command
cbind
mat1
mat1 mat2
mat2
rbind
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
27 Recycling Rule
> 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
Feature Function
Eigen Values eigen
Singular Value Decomposition svd
Least Squares Fitting lsfit
QR decomposition qr
30 Outline
It is a table in R
> df
entries price num
1 cars 8 1
2 trucks 10 2
3 bikes 5 3
34 Accessing an Element
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
36 Reading Data from Files
[[2]]
[1] 8
Scope of variables in R
Function arguments (valid only inside the function)
Local variables (valid only inside the function)
Global variables (balance)
45 Functional Programming: Closures
> exponent <- function (n) {
+ power <- function (x) {
+ x ** n
+ }
+ }
> square <- exponent(2)
> square(4)
[1] 16
area <- 0
+ points <- seq(a, b, length = n + 1)
+
+ area <- 0
+ for (i in seq_len(n)) { Function for
+ area <- area + rule(f, points[i], numerical
points[i + 1]) integration
+ }
+
+ area
+ }
> midpoint <- function(f, a, b) {
+ (b - a) * f((a + b) / 2) Midpoint rule
function passed + }
as an argument > composite(sin, 0, pi, n = 1000, rule =
𝜋
midpoint)
∫ sin ( 𝑥 ) 𝑑𝑥
[1] 2.00000 0
47 Outline
A basic 2D plot:
Plot type
vec1 <-cube(seq(1,100,10)) (overplotted)
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 legend:
legend(1, max(vec1), c(“vec1",“vec2"), cex=0.8, col=c("blue","red"),
pch=21:22, lty=1:2)
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
50 Creating 3-D plots
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