Installing and loading packages

install.packages('ggplot2')
library('ggplot2')

Numeric Variables

a<- 2
b<- 3

ctrl+L clears the screen. ls() shows you the available variables in the Global Environment.

ls()
##   [1] "a"              "A"              "acc.count"     
##   [4] "age.acc"        "AR"             "b"             
##   [7] "B"              "big_num"        "book_list"     
##  [10] "br"             "brk"            "c"             
##  [13] "C"              "C.INV"          "caff.marital"  
##  [16] "d"              "data"           "e"             
##  [19] "expend.lean"    "expend.obese"   "fig"           
##  [22] "fig1"           "first.name"     "fname"         
##  [25] "foo"            "gender.num"     "gender.num.fac"
##  [28] "gender.str"     "gender.str.fac" "h"             
##  [31] "hello"          "i"              "juul"          
##  [34] "L"              "lab"            "lname"         
##  [37] "m"              "M"              "mean.histplot" 
##  [40] "mid.age"        "mob.fac"        "mob.str"       
##  [43] "mob.str.fac"    "ms"             "mu"            
##  [46] "my_data"        "my.csvtable"    "my.list"       
##  [49] "my.list1"       "my.url"         "my.urldata"    
##  [52] "mydata"         "myqplot"        "n"             
##  [55] "n1"             "newdata"        "newrecord"     
##  [58] "news"           "num1"           "num2"          
##  [61] "opar"           "p"              "ptype"         
##  [64] "pvec"           "q"              "randomM1"      
##  [67] "randomM2"       "randomM3"       "s"             
##  [70] "sample_data"    "sample.mean"    "sentiments"    
##  [73] "sigma"          "slices"         "sorted_data"   
##  [76] "spcl"           "str1"           "str2"          
##  [79] "str3"           "stream"         "string1"       
##  [82] "string2"        "text"           "tokens"        
##  [85] "total.caff"     "trace_0"        "trace_1"       
##  [88] "trace_2"        "trace0"         "trace1"        
##  [91] "trace2"         "u"              "url"           
##  [94] "v"              "v1"             "v2"            
##  [97] "vec1"           "vec2"           "word_freq"     
## [100] "x"              "X"              "x.seq"         
## [103] "x.values"       "x1"             "xbar"          
## [106] "xparse"         "y"              "Y"             
## [109] "y.seq"          "y.values"       "y1"            
## [112] "y2"             "Z"

Use typeof() and class() to see the datatype and class respectively.

typeof(a)
## [1] "double"
class(a)
## [1] "numeric"

Logical Variables

c<- a<b
c
## [1] TRUE
class(c)
## [1] "logical"

Standard logical operations are “&” (and), “|” (or), and “!” (negation).

u <- T; v <- F 
u & v
## [1] FALSE
u | v
## [1] TRUE
!u
## [1] FALSE

Character variables

d<- 'Nandini'
e<- 'Manas'
nchar(d)
## [1] 7
class(e)
## [1] "character"
fname = "Rishi"; lname ="Roy" 
paste(fname, lname)
## [1] "Rishi Roy"

Remove a variable using rm().

rm(a)

rm(list=ls()) removes all the variables in the Global Environment. (why?)

Creating vector:

A <- c(1,2,3,4)
print(A)
## [1] 1 2 3 4
B <- c(5,6,7,8)
print(B)
## [1] 5 6 7 8
C <- c(A,B)
print(C)
## [1] 1 2 3 4 5 6 7 8

Sequences:

1:15
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
seq(from=1,to=20,by=3)
## [1]  1  4  7 10 13 16 19
seq(from=1,to=10,length.out=5)
## [1]  1.00  3.25  5.50  7.75 10.00
seq(from=10,to=1,by=-2.25)
## [1] 10.00  7.75  5.50  3.25  1.00

Sequences with repeated entry:

rep(x=4,times=5)
## [1] 4 4 4 4 4
rep(x=c(-1,0,1.6),times=3)
## [1] -1.0  0.0  1.6 -1.0  0.0  1.6 -1.0  0.0  1.6
rep(x=c(-1,1.6),each=3)
## [1] -1.0 -1.0 -1.0  1.6  1.6  1.6
rep(x=c(1,2),times=3,each=2)
##  [1] 1 1 2 2 1 1 2 2 1 1 2 2

Length of a vector:

length(x=c(7,5,9,2))
## [1] 4
length(x=2:11)
## [1] 10

Extracting element from a vector:

vec1 <- c(-3,-2,-1,0,2,4,6,8,10)
vec1[1]
## [1] -3
vec1[3]
## [1] -1
vec1[length(x=vec1)]
## [1] 10
vec1[3:5]
## [1] -1  0  2
vec1[-1]
## [1] -2 -1  0  2  4  6  8 10
vec1[-length(x=vec1)]
## [1] -3 -2 -1  0  2  4  6  8

Element-wise behavior of vector:

vec2 <- c(3,22,-10,0,21,14,6.9,3.8,-10.3)
3*vec2
## [1]   9.0  66.0 -30.0   0.0  63.0  42.0  20.7  11.4 -30.9
vec1-vec2
## [1]  -6.0 -24.0   9.0   0.0 -19.0 -10.0  -0.9   4.2  20.3
vec2*c(1,-1)
## Warning in vec2 * c(1, -1): longer object length is not a multiple of
## shorter object length
## [1]   3.0 -22.0 -10.0   0.0  21.0 -14.0   6.9  -3.8 -10.3
sum(vec2)
## [1] 50.4
prod(vec2)
## [1] 0
X <- c(FALSE, TRUE, FALSE)
v1<- c(9,-3,1)
v2<- c(-4,7,3)
Y <-v1<v2
X
## [1] FALSE  TRUE FALSE
Y
## [1] FALSE  TRUE  TRUE
X & Y   
## [1] FALSE  TRUE FALSE
X|Y     
## [1] FALSE  TRUE  TRUE
X == Y   
## [1]  TRUE  TRUE FALSE
X != Y 
## [1] FALSE FALSE  TRUE
X && Y # only looks at first element of each vector (intersection)
## [1] FALSE
X || Y # only looks at first element of each vector (union)
## [1] FALSE

Sorting a vector:

A<-c(1,-1,3,7,9,0,2,5)
sort(x=A,decreasing = TRUE)
## [1]  9  7  5  3  2  1  0 -1

Matrix and Array

Creating matrix:

A <- matrix(data=c(11,12,21,22),nrow=2,ncol=2)
print(A)
##      [,1] [,2]
## [1,]   11   21
## [2,]   12   22
B <- matrix(data=c(11,12,21,22),nrow=2,ncol=2,byrow=TRUE)
print(B)
##      [,1] [,2]
## [1,]   11   12
## [2,]   21   22

Using rbind and cbind:

rbind(1:3,4:6)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
C <- cbind(c(1,2),c(3,4),5:6)
print(C)
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6

Dimension of matrix:

dim(A)
## [1] 2 2
dim(C)
## [1] 2 3
nrow(C)
## [1] 2
ncol(C)
## [1] 3
dim(C)[2]
## [1] 3

Matrix with random values:

randomM1 <- matrix(rnorm(16), nrow = 4)
print(randomM1)
##            [,1]       [,2]      [,3]       [,4]
## [1,] -1.6070809 -0.6061511 0.6602126 -0.6597701
## [2,] -0.4157518 -0.3047211 2.2734835  2.9191401
## [3,]  0.4220084  0.6295361 1.1734976  0.6774155
## [4,] -0.1517365  0.8951720 0.2877097 -0.6843203
randomM2 <- matrix(runif(16, min = 0, max = 1), nrow = 4)
print(randomM2)
##            [,1]      [,2]      [,3]         [,4]
## [1,] 0.57397056 0.3917717 0.5465087 0.1406190705
## [2,] 0.04959491 0.5169453 0.3931208 0.2892716692
## [3,] 0.37282014 0.1752801 0.6251975 0.0006121558
## [4,] 0.89476805 0.1926055 0.5722048 0.9553637172
randomM3 <- matrix(sample(1:20, 100, replace = TRUE), ncol = 10)
print(randomM3)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1   15   11   15   15   17    9    6    1    17
##  [2,]    4   11    3   18   17   11    7   13    1    15
##  [3,]   14    4   11   15    3    4   16   10   17     6
##  [4,]   10   17   10    6   20   15   12   14   20     9
##  [5,]   13   14   10    8   11   18   11   20   19     1
##  [6,]   13   14   18    9   20    7    7    6   16     3
##  [7,]   10   18    3   19    7    2    7    2   17     6
##  [8,]   19    8    4    8    4    5    5   20    6    20
##  [9,]   11   13   15   12   13   19    6   10   13    18
## [10,]   15   20    2    3   16   15    1   15   11    17

There are several more: rexp(), rpois(), rbinom() etc.

Identity and zero matrices:

diag(4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
Z <- matrix(0, 4, 5)
print(Z)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    0    0    0    0
## [3,]    0    0    0    0    0
## [4,]    0    0    0    0    0

Extracting element, row, column and diagonal from a matrix:

randomM3[5,5]
## [1] 11
randomM3[,2]
##  [1] 15 11  4 17 14 14 18  8 13 20
randomM3[1,]
##  [1]  1 15 11 15 15 17  9  6  1 17
randomM3[c(3,1),2:3]
##      [,1] [,2]
## [1,]    4   11
## [2,]   15   11
diag(x=randomM3)
##  [1]  1 11 11  6 11  7  7 20 13 17

Omitting and Overwriting:

C[,-1]
##      [,1] [,2]
## [1,]    3    5
## [2,]    4    6
randomM3[-c(1,2,3),-c(4,5,6)]
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   10   17   10   12   14   20    9
## [2,]   13   14   10   11   20   19    1
## [3,]   13   14   18    7    6   16    3
## [4,]   10   18    3    7    2   17    6
## [5,]   19    8    4    5   20    6   20
## [6,]   11   13   15    6   10   13   18
## [7,]   15   20    2    1   15   11   17
B <- randomM3[-c(5:10),-c(5:10)]
print(B)
##      [,1] [,2] [,3] [,4]
## [1,]    1   15   11   15
## [2,]    4   11    3   18
## [3,]   14    4   11   15
## [4,]   10   17   10    6
B[,1]=c(1,2,3,4)
print(B)
##      [,1] [,2] [,3] [,4]
## [1,]    1   15   11   15
## [2,]    2   11    3   18
## [3,]    3    4   11   15
## [4,]    4   17   10    6
B[c(1,4),c(2,3)] <- 0
print(B)
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0   15
## [2,]    2   11    3   18
## [3,]    3    4   11   15
## [4,]    4    0    0    6
diag(Z) <- rep(x=1,times=4)
print(Z)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0

Operations between matrices:

A <- cbind(c(10,20,30),c(4,5,6))
B <- cbind(c(10,30),c(4,9), c(10,4))
print(B)
##      [,1] [,2] [,3]
## [1,]   10    4   10
## [2,]   30    9    4
3*B
##      [,1] [,2] [,3]
## [1,]   30   12   30
## [2,]   90   27   12
dim(A)
## [1] 3 2
dim(B)
## [1] 2 3
C <- A%*%B
print(C)
##      [,1] [,2] [,3]
## [1,]  220   76  116
## [2,]  350  125  220
## [3,]  480  174  324
det(x=C)
## [1] 6.514789e-11
diag(C) <- c(1,1,1)
det(C)
## [1] 14969441
C.INV <- solve(C)
C%*%C.INV
##              [,1]          [,2]         [,3]
## [1,] 1.000000e+00 -3.469447e-17 4.163336e-17
## [2,] 2.775558e-17  1.000000e+00 1.249001e-16
## [3,] 2.949030e-17  0.000000e+00 1.000000e+00
round(C%*%C.INV,digits = 15)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Multidimensional Arrays:

AR <- array(data=1:24,dim=c(3,4,2))
print(AR)
## , , 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24