4 - DataTypes Vector Matrices Operators
4 - DataTypes Vector Matrices Operators
Vector, Matrices
and Operators
1
Course Objectives
2
➢ Gain an in-depth understanding of data structure used in
R and learn to import export data in R
3
R Data Types
01 Vectors
02 Lists
03 Matrices
04 DataFrame
05 Factors
4
One of the key features of R is that it can handle complex statistical
operations in an easy and optimised way.
5
❑ Lists – Lists store collections of objects when vectors are of same
type and length in a matrix.
6
Vectors in R
In R programming, the very basic data types are the R-objects called
vectors which hold elements of different classes.
# Create a vector
apple <- c('red','green',"yellow")
print(apple)
7
Vectors in R
❑ In R using the function, typeof() one can check the data type of
vector
>c(2, 3, 5) [1] 2 3 5
[1] 2 3 5
>length(c("aa", "bb", "cc", "dd", "ee"))
[1] 5
8
Vectors in R
9
Vectors in R
10
Vectors in R
11
List in R
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
12
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
[[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function (x).Primitive("sin")
13
Matrices in R
A matrix is a two-dimensional rectangular data set. It can be created
using a vector input to the matrix function.
# Create a matrix
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
14
Arrays in R
❑ While matrices are confined to two dimensions, arrays can be of any
number of dimensions.
❑ In the below example we create an array with two elements which are
3x3 matrices each.
# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
15
Arrays in R
# Create an array.
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
,,1
[,1] [,2] [,3]
[1,] "green" "yellow" "green"
[2,] "yellow" "green" "yellow"
[3,] "green" "yellow" "green“
,,2
[,1] [,2] [,3]
[1,] "yellow" "green" "yellow"
[2,] "green" "yellow" "green"
[3,] "yellow" "green" "yellow"
16
Factors in R
❑ Factors are the r-objects which are created using a vector.
❑ It stores the vector along with the distinct values of the elements in
the vector as labels.
17
Factors in R
# Create a vector
apple_colors <-
c('green','green','yellow','red','red','red','
green’)
o/p
❑ The first column can be numeric while the second column can be
character and third column can be logical.
19
Data Frames in R
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
1 2
Arithmetic Relational
Operators Operators
3 4
Logical Assignment
Operators Operators
21
Arithmetic Operators
These operators are used to carry out mathematical operations like
addition and multiplication. Here is a list of arithmetic operators available
in R.
22
Examples
>x <- 5
>y <- 16
>x+y
>[1] 21
>x-y
>[1] -11
>x*y
>[1] 80
>y/x
>[1] 3.2
>y%/%x
>[1] 3
>y%%x
>[1] 1
>y^x
>[1] 1048576
23
Relational Operators
Relational operators are used to compare between values. Here is a list
of relational operators available in R.
24
Examples
>x <- 5
>y <- 16
>x<y
>[1] TRUE
>x>y
>[1] FALSE
>x<=5
>[1] TRUE
> y>=20
>[1] FALSE
>y == 16
>[1] TRUE
>x != 5
>[1] FALSE
25
Operation on Vectors
Warning message:
In x + c(1, 2, 3) :
longer object length is not a multiple of shorter object length
27
Logical Operators
Logical operators are used to carry out Boolean operations like AND, OR
etc.
28
❑ Operators & and | perform element-wise operation producing
result having length of the longer operand.
❑ But && and || examines only the first element of the operands
resulting into a single length logical vector.
30
Examples
> x <- 5
>x
[1] 5
>x =9
>x
[1] 9
> 10 -> x
>x [1]
10
31
Examples
>Console
# An example
>x <- c(1:10)
>x[(x>8) | (x<5)]
# yields 1 2 3 4 9 10
# How it works
>x <-
>xc(1:10)
1 2 3 4 5 6 7 8 9 10
>x > 8
F F F F F F F F TT
32
Examples
>x < 5
T T T T F F F F FF
>x > 8 | x < 5
TTTTFFFFTT
>x[c(T,T,T,T,F,F,F,F,T,T)]
1 2 3 4 9 10
33