Creating and Manipulating Vectors, Matrices, and Arrays in R, Study notes of Introduction to Database Management Systems

A comprehensive guide on creating and manipulating vectors, matrices, and arrays in r, a popular programming language. It covers the creation of numeric, character, and logical vectors, as well as the creation and manipulation of matrices and arrays. The document also explains vector arithmetic operations, vector subsetting, and matrix setting. It further delves into accessing elements of vectors and matrices, and performing arithmetic operations on them.

Typology: Study notes

2023/2024

Uploaded on 03/01/2024

21q91a6782-gummadeela-madhuvamshi
21q91a6782-gummadeela-madhuvamshi 🇮🇳

2 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT - III Vectors: Creating and Naming Vectors, Vector Arithmetic,
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
A vector is simply a list of items that are of the same type.
A vector is a basic data structure which plays an important role in R
programming.
In R, a sequence of elements which share the same data type is known
as vector. A vector supports logical, integer, double, character, complex,
or raw data type. The elements which are contained in vector known
as components of the vector. We can check the type of vector with the
help of the typeof() function.
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Creating and Manipulating Vectors, Matrices, and Arrays in R and more Study notes Introduction to Database Management Systems in PDF only on Docsity!

UNIT - III Vectors: Creating and Naming Vectors, Vector Arithmetic, 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

 A vector is simply a list of items that are of the same type.  A vector is a basic data structure which plays an important role in R programming.  In R, a sequence of elements which share the same data type is known as vector. A vector supports logical, integer, double, character, complex, or raw data type. The elements which are contained in vector known as components of the vector. We can check the type of vector with the help of the typeof() function.  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  # R program to create numeric Vectors# creation of vectors using c() function.v1 <- c(4, 5, 6, 7)# display type of vectortypeof(v1)# by using 'L' we can specify that we want integer values.v2 <- c(1L, 4L, 2L, 5L)# display type of vectortypeof(v2) O/P: [1] "double" [1] "integer"

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:

  1. Using c() Function
  2. Using the colon(:) operator
  3. Using the seq() function
  4. Using assign() function Using c() Function The c function in R programming stands for 'combine. ' This function is used to get the output by giving parameters inside the function. EX: 1  # R program to create Vectors# we can use the c function# to combine the values as a vector.# By default the type will be doubleX <- c(61, 4, 21, 67, 89, 2)cat('using c function', X)# print the vales option 1X# print the vales option 2# print(X)

O/P: using c function 61 4 21 67 89 2

  1. Using the colon(:) operator Colon operator (":") in R is a function that generates regular sequences. It is most commonly used in for loops, to index and to create a vector with increasing or decreasing sequence. It is a binary operator i.e. it takes two arguments. Syntax : z <- x:y EX: 1  # Vector with numerical values in a sequence numbers <- 1 : 10 numbers EX: 2

Vector with numerical decimals in a sequence

numbers1 <- 1.5:6. numbers

  1. Using the seq() function In R, we can create a vector with the help of the seq() function. A sequence function creates a sequence of elements as a vector. The seq() function is used in two ways, i.e., by setting step size with ?by' parameter or specifying the length of the vector(sequence) with the 'length.out' feature.

EX:

Creating a sequence from 5 to 13.

v <- 5 : 13 print(v)

Creating a sequence from 6.6 to 12.6.

v <- 6.6:12. print(v) EX: **# R program to illustrate

Assigning vectors

Assigning a vector using

seq() function

V = seq(1, 3, by=0.2)

Printing the vector

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. 4 Using assign() function The assign() function takes the following mandatory parameter values: x : This represents the variable name that is given as a character string. value : This is the value to be assigned to the x variable. EX:  assign("vec2",c(6,7,8,9,10))vec 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:a <- c(10, 20, 30, 40, 50)b <- c(1, 3, 5, 7, 9)result <- a + b

Ex:a <- c(10, 20, 30, 40, 50)b <- c(1, 3, 5, 7, 9)result <- a * bprint(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.Ra <- c(10, 20, 30, 40, 50)b <- c(1, 3, 5, 7, 9)result <- a / bprint(result) Output [1] 10.000000 6.666667 6.000000 5.714286 5.

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.

  1. Indexing with integer vector On integer vector, indexing is performed in the same way as we have applied in C, C++, and java. There is only one difference, i.e., in C, C++, and java the indexing starts from 0, but in R, the indexing starts from 1. Like other programming languages, we perform indexing by specifying an integer value in square braces [] next to our vector. Example:

**1. seq_vec<-seq(1,4,length.out= 6 )

  1. seq_vec
  2. seq_vec[2]**

Output [1] 1 3 4 6

Vector sub setting

 Vectors are basic objects in R and they can be subsetted using the [ operator.  EX: > x <- c("a", "b", "c", "c", "d", "a") > x[1] ## Extract the first element [ 1 ] "a"  EX > x[ 2 ] ## Extract the second element [ 1 ] "b"  The [ operator can be used to extract multiple elements of a vector by passing the operator an integer sequence. Here we extract the first four elements of the vector. > x[1:4] [ 1 ] "a" "b" "c" "c"  The sequence does not have to be in order; you can specify any arbitrary integer vector.  EX > x[c( 1 , 3 , 4 )] [ 1 ] "a" "c" "c"

Matrices: Creating and Naming Matrices

 In R, a two-dimensional rectangular data set is known as a matrix. A matrix is created with the help of the vector input to the matrix function. On R matrices, we can perform addition, subtraction, multiplication, and division operation.  In the R matrix, elements are arranged in a fixed number of rows and columns. The matrix elements are the real numbers. In R, we use matrix function, which can easily reproduce the memory representation of the matrix. In the R matrix, all the elements must share a common basic type.  To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector.  You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order  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.  A Matrix is created using the matrix() function. Syntax  The basic syntax for creating a matrix in R is −  matrix(data, nrow, ncol, byrow, dimnames)

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. Example to understand how matrix function is used to create a matrix and arrange the elements sequentially by row or column. EX:  P <- matrix(c(5:16), nrow = 4, byrow = TRUE)print(P)# Arranging elements sequentially by column.Q <- matrix(c(3:14), nrow = 4, byrow = FALSE)print(Q)# Defining the column and row names.row_names = c("row1", "row2", "row3", "row4")col_names = c("col1", "col2", "col3")R <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_names))print(R) 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 col row1 3 4 5 row2 6 7 8 row3 9 10 11 row4 12 13 14 Accessing matrix elements in R 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# Defining the column and row names.row_names = c("row1", "row2", "row3", "row4")ccol_names = c("col1", "col2", "col3")#Creating matrixR <- matrix(c(5:16), nrow = 4 , byrow = TRUE, dimnames = list(row_names , col_names))print(R)

Assign a single element  In matrix modification, the first method is to assign a single element to the matrix at a particular position. By assigning a new value to that position, the old value will get replaced with the new one. This modification technique is quite simple to perform matrix modification. The basic syntax for it is as follows:

  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# Defining the column and row names.row_names = c("row1", "row2", "row3", "row4")ccol_names = c("col1", "col2", "col3")

 R <-

matrix(c(5:16), nrow = 4 , byrow = TRUE, dimnames = list(row_names , col_names))print(R)#Assigning value 20 to the element at 3d roe and 2nd columnR[3,2]<- 20print(R) Output col1 col2 col row1 5 6 7 row2 8 9 10 row3 11 12 13 row4 14 15 16 col1 col2 col row1 5 6 7 row2 8 9 10 row3 11 20 13 row4 14 15 16 Use of Relational Operator  R provides another way to perform matrix medication. In this method, we used some relational operators like >, <, ==. Like the first method, the second method is quite simple to use. Let see an example to understand how this method modifies the matrix.