Académique Documents
Professionnel Documents
Culture Documents
Email: pritamr@iimidr.ac.in
Office: A - 102, Phone: 512
Session - 1: R as a Calculator,
Arrays, Lists and Summary statistics
A yog-ic perspective !!
SAR
=
Applied Statistics
+
R – Easy yet powerful implementation tool
– https://cran.r-project.org/manuals.html
Basic Arithmetic
help(exp) ≈ ?exp
help(”+”) ≈ ?”+”
help.search(”sin”) ≈ ??sin
Remark
Saving output
– sink(”myoutput.txt”)
– sink()
# – comment
Defining variables
x=2 or x <– 2
”2 –> x” also works
x = ”bob” / x = ’bob’
x = ”Statistical Applications with R”
bob = (2+3/4)*5+sin(30)
cos = (2+3/4)*5+sin(30)
varnames = paste(”X”,1:10,sep=””)
Vectors - 1
x = c(32,54,54,32,21,1.23)
x = 2:10
x = -2.5:5.3
x=c(1,2,34,-23,”bob”)
x=c(1,2,34,-23, x)
Vectors - 2
x = seq()
x = numeric(5)
y = rep(-0.1,3) ; y = rep(x,3)
Vector arithmetic:
x+2 ; x*2 ; x/2 ;
x + y ; 1/x ; sin(x) ; log(x) ; x ˆ2
subsetting
Conditions:
y=x[!is.na(x)]
> is.nan(x) / is.infinite(x) / is.finite(x)
x[x < 0] = 0
use of multiple conditions via (& or &&) and (| or k)
Matrices - 1
x = matrix(0,2,3) ; A = array(0,dim=c(2,3))
x = matrix(1:5,ncol=2,byrow=T)
arithmetic:
x+2 ; x*2 ; x/2 ;
x + y ; 1/x ; sin(x) ; log(x) ; x ˆ2
Matrices - 2
subsetting
x[1,2] ; x[,2] ; x[,c(1,3)]
x[-1,2] ; x[6]
x[c(),c()]
A[condition-1, condition-2]
Important functions
For a vector: x
length(x)
sum(x) ; prod(x) ;
min(x); max(x)
For a matrix: A
dim(A)
rowSums(A) ; colSums(A)
Exercise
solve the system of equation: Ax = b
Important functions
apply() / tapply()
cbind() / rbind()
which() / which.max()
Factors
values = c(1,2,1,1,2,3,3)
vlabels = factor(values, levels=1:3)
levels(vlabels) = c(”low”,”med”,”high”)
Important commands
q()
getwd() / setwd()
save.image(”mycommands.RData”)
load(”mycommands.RData”)
Lists
Lists
xx = NULL;
xx$a = scalar
xx$b = vector
xx$c = matrix
xx$d = character string
yy = NULL
define yy[[i]] for i=1,2,...,k
Lists
creating a list
a = c() # numeric
b = c() # characters
d = matrix
mylist = list(a,b,d)
Combining lists
list3 = append(list1,list2) / c(list1, list2)
Deleting entries
list3[[2]] = NULL
Practice Exercises
Debugging Exercise
x = c(10,21,45,56,67,78,24,47)
> print(x)
> length(x) <- 10
> print(x)
Debugging Exercise
Which of the following is incorrect?
(a) 10 + 5
(b) 10 ∗ 5
(c) 10 − 5
(d) 10 / 5
(e) 10 % 5
Exercise
Guess the possible output
(a) 1:10
(b) 10:1
(c) 4*10:2
(d) seq(1:5,by=0.2)
(e) 2:20 + 20:2
Exercise
Construct the following matrix using R
2 7 12 17 3
3 8 13 18 4
4 9 14 19 5
5 10 15 20 6
6 11 16 2 7
Summary Statistics
Summary
mean, median, mode, variance, sd,
Find mode of x via
“sort(unique(x))[which.max(table(x))]”
sort, order,
order(x) contains the location of sorted-x (in increasing
order) in the original x vector
e.g., x = c(2,34,12,3,5) gives order(x) = [1 4 5 3 2], i.e.,
2 appears in the 1st location, 3 appears in 4th location, 5
appears in the 5th location, 12 appears in the 3rd
location and 34 appears in the 2nd location.
Summary
correlation (spearman, pearson, ...)
na.rm=TRUE
Summary
skewness, kurtosis
>install.packages(”moments”)
>library(moments)
>skewness(x)
>moment(x)
Homework