March, 2018

Vector

Variables

x1 = 3.5
typeof(x1)
## [1] "double"
x2 = 3
typeof(x2)
## [1] "double"
x3 = 3L
typeof(x3)
## [1] "integer"

Variables

x4 = "a"
typeof(x4)
## [1] "character"
x5 = TRUE
typeof(x5)
## [1] "logical"

Variables

x6 = x1 + x3
typeof(x6)
## [1] "double"
  • R language is not strict tp the type of variables. It provides flexible type casting, and thus we have to be care about the casting in R.

Creating vector

  • The function c() creates 1-dimensional array called of vector in mathematics.
x = c(3)
print(x)
## [1] 3
x = c(88, 15, 12, 13)
x
## [1] 88 15 12 13

Creating vector

  • To create a series of consecutive intergers the colon function : is frequentyly used.
1:5
## [1] 1 2 3 4 5
3:(-2)
## [1]  3  2  1  0 -1 -2
(3.1):(5.5)
## [1] 3.1 4.1 5.1

Creating vector

  • A variable is basically treated as the vector with length 1.
  • c() can take multiple vectors. For example,
x1 = 1:3
x2 = 10:5
x3 = c(x2,x1)
x3
## [1] 10  9  8  7  6  5  1  2  3

Useful functions to create vectors

  • seq()
y = seq(from = 12, to = 30, by = 2)
y
##  [1] 12 14 16 18 20 22 24 26 28 30
y = seq(12, 30, length = 19)
y
##  [1] 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Useful functions to create vectors

  • rep()
x = rep(8, 4)
x
## [1] 8 8 8 8
x = rep( c(5, 12, 13), 3)
x
## [1]  5 12 13  5 12 13  5 12 13
x = rep( c(5, 12, 13), each = 3)
x
## [1]  5  5  5 12 12 12 13 13 13

Slicing vector I

  • There are mainly two method for selecting elements in vector. First we will choose elements by the index of vector.
  • The bracket [] is used to specifying indices.
x = 5:8
x1 = x[1:3]
x1
## [1] 5 6 7
x2 = c(x1[1:3], 10, x[4])
x2
## [1]  5  6  7 10  8

Slicing vector I

y = c(1.2, 3.9, 0.4, 0.12)
y[c(1, 3)]
## [1] 1.2 0.4
y[2:3]
## [1] 3.9 0.4
v = 3:4
y[v]
## [1] 0.40 0.12
y[c(1, 1, 2)]
## [1] 1.2 1.2 3.9

Slicing vector I

  • The index with negative signs selects all elements with excluding the correspondent elements to the index.
y[-c(1,2)]
## [1] 0.40 0.12
y[-length(y)]
## [1] 1.2 3.9 0.4

Length of vector

  • length() is used to return the length of a vector.
x = 3:6
length(x)
## [1] 4
1:length(x)
## [1] 1 2 3 4

Operation on vector

  • Basically, the arithmetic operation is defined elementwisely.
x1 = c(5,0, -4)
x2 = c(1, 2, 2)
x1+x2
## [1]  6  2 -2
x1*x2
## [1]  5  0 -8
x1/x2
## [1]  5  0 -2

Operation on vector

  • If the elementwise operation is ended before completion, the elements in the vector with shorter length is reused.
x1 + 3
## [1]  8  3 -1
x1*2
## [1] 10  0 -8
x1 = c(5,0,-4,2)
x2 = c(2,1)
x1/x2
## [1]  2.5  0.0 -2.0  2.0

Order of operations

y = 1:10-1
y
##  [1] 0 1 2 3 4 5 6 7 8 9
y = 1:(10-1)
y
## [1] 1 2 3 4 5 6 7 8 9

useful operation

x <- 11
x^2
## [1] 121
x%/%5
## [1] 2
x%%5
## [1] 1

Logical operation

6<=7
## [1] TRUE
6==7
## [1] FALSE
6!=7
## [1] TRUE

Logical operation

z = c(5, 2, -3, 8)
z^2
## [1] 25  4  9 64
z^2 > 8
## [1]  TRUE FALSE  TRUE  TRUE

Slicing vector II (filtering)

z[z>0]
## [1] 5 2 8
z[z^2>8]
## [1]  5 -3  8

Slicing vector II (filtering)

x = c(3,1,4,1)
x[x>5]
## numeric(0)
which(x>5)
## integer(0)
x[which(x>5)]
## numeric(0)

%in% function

1 %in% c(2,1,4)
## [1] TRUE
c(1,5) %in% c(2,1,5)
## [1] TRUE TRUE
c(1,5,3) %in% c(2,1,5)
## [1]  TRUE  TRUE FALSE
x = c(3,1,4,1)
x%in% c(2,1,4)
## [1] FALSE  TRUE  TRUE  TRUE

match() function

match(1 ,c(2,1,4))
## [1] 2
match(c(1,4),c(2,1,4))
## [1] 2 3
x <- c(3,1,4,1)
match(x ,c(2,1,4))
## [1] NA  2  3  2

Matrix

Create matrix

  • Matrix is 2-dimensional array in R. It can be treated as 1-dimensional vector by ignoring the index in row and column.
y = matrix( c(1, 2, 3, 4), nrow = 2, ncol = 2)
y
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
y = matrix( c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T)
y
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

Create matrix

y = matrix(c(1,3,4,5,1,3,4,1),4,2)
y[1, 1]
## [1] 1
y[, 1]
## [1] 1 3 4 5
y[-2,]
##      [,1] [,2]
## [1,]    1    1
## [2,]    4    4
## [3,]    5    1

Matrix function

class(y)
## [1] "matrix"
dim(y)
## [1] 4 2
ncol(y)
## [1] 2
nrow(y)
## [1] 4

column bind and row bind

one = rep(1,4)
z = matrix( c(5:8, rep(-1,4), rep(0,4)), 4, 3)
cbind(one, z)
##      one       
## [1,]   1 5 -1 0
## [2,]   1 6 -1 0
## [3,]   1 7 -1 0
## [4,]   1 8 -1 0

column bind and row bind

z = rbind(2, z)
z
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    5   -1    0
## [3,]    6   -1    0
## [4,]    7   -1    0
## [5,]    8   -1    0

Dataframe

Create dataframe

  • Array including vector and matrix can contain only same type of elememts. If a different type of element is added then R casts the arrary to the type.
x = 1:10
x[1] = "a"
typeof(x[2])
## [1] "character"
x = matrix(1:4,2,2)
x[2,2] = "b"
typeof(x)
## [1] "character"

Create dataframe

  • Dataframe seems to be Matrix, but it has different type of data for each column while Matrix has only the same type of values.
kids = c("Jack", "Jill")
ages = c(12, 10)
d = data.frame(kids, ages, stringsAsFactors = F)
d 
##   kids ages
## 1 Jack   12
## 2 Jill   10
str(d)
## 'data.frame':    2 obs. of  2 variables:
##  $ kids: chr  "Jack" "Jill"
##  $ ages: num  12 10

Selecting the elements

  • selectin column
d$ages
## [1] 12 10
class(d$ages)
## [1] "numeric"
names(d)
## [1] "kids" "ages"

Selecting the elements

  • selecting by index
d[1,]
##   kids ages
## 1 Jack   12
class(d[1,])
## [1] "data.frame"

cbind

A = data.frame(x1 = rep(0,10), x2 = rep('b',10))
B = data.frame(x3 = rep(1,10), x2 = rep('d',10))
AB = cbind(A,B)
head(AB)
##   x1 x2 x3 x2
## 1  0  b  1  d
## 2  0  b  1  d
## 3  0  b  1  d
## 4  0  b  1  d
## 5  0  b  1  d
## 6  0  b  1  d