





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 45
This page cannot be seen from the preview
Don't miss anything!






































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
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 vector typeof(v1) # by using 'L' we can specify that we want integer values. v2 <- c(1L, 4L, 2L, 5L) # display type of vector typeof(v2) O/P: [1] "double" [1] "integer"
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:
O/P: using c function 61 4 21 67 89 2
numbers1 <- 1.5:6. numbers
v <- 5 : 13 print(v)
v <- 6.6:12. print(v) EX: **# R program to illustrate
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. 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
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
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 * 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.
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. seq_vec<-seq(1,4,length.out= 6 )
Output [1] 1 3 4 6
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"
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
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.
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:
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 column R[3,2]<- 20 print(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.