unit 3
unit 3
Vector sub setting, Matrices: Creating and Naming Matrices, Matrix Sub
setting, Arrays, Class. Factors and Data Frames: Introduction to Factors:
Factor Levels, Summarizing a Factor, Ordered Factors, Comparing
Ordered Factors, Introduction to Data Frame, subsetting of Data Frames,
Extending Data Frames, Sorting Data Frames. Lists: Introduction,
creating a List: Creating a Named List, Accessing List Elements,
Manipulating List Elements, Merging Lists, Converting Lists to Vectors
Vectors
To combine the list of items to a vector, use the c() function and separate
the items by a comma.
Vectors in R are the same as the arrays in C language which are used to
hold multiple data values of the same type. One major key point is that
in R the indexing of the vector will start from ‘1’ and not from ‘0’. We can
create numeric vectors and character vectors as well.
The length is an important property of a vector. A vector length is
basically the number of elements in the vector, and it is calculated with
the help of the length() function.
Types of vectors
Vectors are of different types which are used in R.
1.Numeric vectors
Numeric vectors are those which contain numeric values such as integer,
float, etc.
Ex: 1
Character vectors
Character vectors contain alphanumeric values and special characters.
Output:
[1] "character"
Ex :3
Logical vectors
Logical vectors contain boolean values such as TRUE, FALSE and NA for
Null values.
Output:
[1] "logical"
Creating and Naming Vectors
we use c() function to create a vector. This function returns a one-
dimensional array or simply vector. The c() function is a generic
function which combines its argument.
All arguments are restricted with a common data type which is the
type of the returned value. There are various other ways also there
Types:
numbers
EX: 2
# Vector with numerical decimals in a sequence
numbers1 <- 1.5:6.5
numbers1
seq_vec<-seq(1,4,by=0.5)
seq_vec
class(seq_vec)
Output
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0
[1] "numeric"
EX: 2
seq_vec<-seq(1,4,length.out=10)
seq_vec
class(seq_vec)
Output
[1] 1.000000 1.333333 1.666667 2.000000 2.333333 2.666667
3.000000 3.333333
[9] 3.666667 4.000000
[1] "numeric"
EX: 3
seq_vec<-seq(1,4,length.out=5)
seq_vec
class(seq_vec)
Output
[1] 1.00 1.75 2.50 3.25 4.00
[1] "numeric"
EX:4
# R program to illustrate
# Assigning vectors
# Assigning a vector using
# seq() function
V = seq(1, 3, by=0.2)
print(V)
Output
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0
Output
[1] 6 7 8 9 10
Vector Arithmetic Operations
We can perform arithmetic operations on vectors, like addition,
subtraction, multiplication and division.
Please note that the two vectors should be of same length and same type.
Or one of the vectors can be an atomic value of same type.
If the vectors are not of same length, then Vector Recycling happens
implicitly.
Vector Recycling:
If two vectors are of unequal length, the shorter one will be recycled in
order to match the longer vector. For example, the following
vectors u and v have different lengths, and their sum is computed by
recycling values of the shorter vector u.
> u = c(10, 20, 30)
> v = c(1, 2, 3, 4, 5, 6, 7, 8, 9)
>u+v
[1] 11 22 33 14 25 36 17 28 39
Types:
1. Addition
Addition operator takes two vectors as operands, and returns the result
of sum of two vectors.
a+b
Example
In the following program, we create two integer vectors and add them using
Addition Operator.
Ex:1
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a + b
print(result)
Output
[1] 11 23 35 47 59
2. Subtraction
Subtraction operator takes two vectors as operands, and returns the
result of difference of two vectors.
a-b
Example
In the following program, we create two integer vectors and find their different
using Subtraction Operator.
Ex:1
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a - b
print(result)
Output
[1] 9 17 25 33 41
3.Multiplication
Multiplication operator takes two vectors as operands, and returns the
result of product of two vectors.
a*b
Example
In the following program, we create two integer vectors and find their
product using Multiplication Operator.
Ex:
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a * b
print(result)
Output
[1] 10 60 150 280 450
4.Division
Division operator takes two vectors are operands, and returns the result
of division of two vectors.
a+b
Example
In the following program, we create two integer vectors and divide them using
Division Operator.
Example.R
a <- c(10, 20, 30, 40, 50)
b <- c(1, 3, 5, 7, 9)
result <- a / b
print(result)
Output
[1] 10.000000 6.666667 6.000000 5.714286 5.555556
Accessing elements of vectors
We can access the elements of a vector with the help of vector indexing.
Indexing denotes the position where the value in a vector is stored. Indexing
will be performed with the help of integer, character, or logic.
Example:
1. seq_vec<-seq(1,4,length.out=6)
2. seq_vec
3. seq_vec[2]
Output
Example:
1. char_vec<-c("shubham"=22,"arpita"=23,"vaishali"=25)
2. char_vec
3. char_vec["arpita"]
Output
Example:
1. a<-c(1,2,3,4,5,6)
2. a[c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE)]
Output
[1] 1 3 4 6
EX
Matrices are the R objects in which the elements are arranged in a two-
dimensional rectangular layout. They contain elements of the same
atomic types. Though we can create a matrix containing only characters
or only logical values, they are not of much use. We use matrices
containing numeric elements to be used in mathematical calculations.
Syntax
data is the input vector which becomes the data elements of the matrix.
nrow is the number of rows to be created.
ncol is the number of columns to be created.
byrow is a logical clue. If TRUE then the input vector elements are
arranged by row.( byrow is a logical variable. Matrices are by default
column-wise. By setting byrow as TRUE, we can arrange the data row-
wise in the matrix)
dimname is the names assigned to the rows and columns(takes two
character arrays as input for row names and column names).
data
The first argument in matrix function is data. It is the input vector which is the
data elements of the matrix.
nrow
The second argument is the number of rows which we want to create in the
matrix.
ncol
The third argument is the number of columns which we want to create in the
matrix.
byrow
The byrow parameter is a logical clue. If its value is true, then the input vector
elements are arranged by row.
dim_name
The dim_name parameter is the name assigned to the rows and columns.
EX:
Output
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 8 9 10
[3,] 11 12 13
[4,] 14 15 16
[,1] [,2] [,3]
[1,] 3 7 11
[2,] 4 8 12
[3,] 5 9 13
[4,] 6 10 14
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14
There are three ways to access the elements from the matrix.
1. We can access the element which presents on nth row and mth column.
2. We can access all the elements of the matrix which are present on the
nth row.
3. We can also access all the elements of the matrix which are present on
the mth column.
Example
Output
[1] 12
1. matrix[n, m]<-y
Here, n and m are the rows and columns of the element, respectively. And, y is
the value which we assign to modify our matrix.
Example
Output
Output
Output
Example 1
R <-
matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names
, col_names))
print(R)
#Adding row
rbind(R,c(17,18,19))
#Adding column
cbind(R,c(17,18,19,20))
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 5 8 11 14 6 9 12 15 7 10 13 16
Matrix operations
Example 1
#Addition
sum<-R+S
print(sum)
#Subtraction
sub<-R-S
print(sub)
#Multiplication
mul<-R*S
print(mul)
#Multiplication by constant
mul1<-R*12
print(mul1)
#Division
div<-R/S
print(div)
Output
Applications of matrix
1. In geology, Matrices takes surveys and plot graphs, statistics, and used
to study in different fields.
2. Matrix is the representation method which helps in plotting common
survey things.
3. In robotics and automation, Matrices have the topmost elements for the
robot movements.
4. Matrices are mainly used in calculating the gross domestic products in
Economics, and it also helps in calculating the capability of goods and
products.
5. In computer-based application, matrices play a crucial role in the
creation of realistic seeming motion.
Matrix subsetting
A matrix is subset with two arguments within single brackets, [], and
separated by a comma. The first argument specifies the rows, and the
second the columns.
EX:
Output
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
A B C
[1,] 1 4 7
[2,] 2 5 8
Ex: 2
Output
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
A B C
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Note:
R Arrays
R Array Syntax
1. array_name <-
array(data, dim= (row_size, column_size, matrices, dim_names))
data
matrices
row_size
column_size
dim_names
Uni-Dimensional Array
A vector is a uni-dimensional array, which is specified by a
single dimension, length. A Vector can be created using ‘c()‘
function. A list of values is passed to the c() function to create
a vector.
EX:
print (vec1)
Multi-Dimensional Array
A two-dimensional matrix is an array specified by a fixed
number of rows and columns, each containing the same data
type. A matrix is created by using array() function to which
the values and the dimensions are passed.
EX:1
print(arr)
Output
,,1
,,2
[,1] [,2] [,3]
[1,] 8 10 12
[2,] 9 11 13
Creation of arrays
Example
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
print(res)
Output
, , Matrix1
, , Matrix2
Accessing arrays
The arrays can be accessed by using indices for different
dimensions separated by commas. Different components can
be specified by any combination of elements’ names or
positions.
Accessing Uni-Dimensional Array
The elements can be accessed by using indexes of the
corresponding elements.
EX:1
# accessing elements
Output
Vector is : 1 2 3 4 5 6 7 8 9 10Third element of vector is : 3
EX: 1
print(array1)
# access entire elements at 2nd column of 1st matrix
cat("\n2nd Column Elements of 1st matrix:",
array1[,c(2),1])
Output
,,1
,,2
EX: 1
array1
array2
print(result)
Output
,,1
,,2
,,1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
,,2
EX: 1
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
#Taking the vectors as input to the array1
print(res1)
print(result)
Output
,,1
,,2
Output
,,1
,,2
[1] 18 66 84
EX: 2
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
print(res1)
print(result)
Output
,,1
,,2