Bhukya annamaiah EXPERIMENT -1 Date- 06|10|23 Aim: Entrying data using vectors and working with r. Problem : Define a vector of decimal data, whose elements are: 2.345612, 1.256701, 2.19875,0.123765,4.132895,7.312678,0.123453,4.103254,3.198341 and perform the following tasks: I. II. III. IV. V. VI. VII. VIII. Check the length of this vector and also convert its elements to 3 decimal places. Extract the 4th and 6th entry. Extract all those entries whose values are greater than equal to 3. Check whether it’s a numeric vector, character vector or integer vector? Calculate the square root and natural log of each entry in a single command. Convert this vector to character and integer vector, thereafter perform the checking by using testing functions available in R. Name the vector elements as X1, X2, X3, …. By using the names subsets, the vector for the values of X3, X6 and X9. Theory and methodology: A vector can be created using an in-built function in R called c(). Elements must be comma-separated. I. Checking the Length and Rounding: - `length(my_vector)` is used to find the number of elements in the vector. - `round(my_vector, 3)` is used to round each element of the vector to three decimal places. Bhukya annamaiah II. Extracting Specific Entries: - In R, you can extract specific elements from a vector using square brackets. III. Extracting Entries Greater Than or Equal to 3: - ` Done using logical indexing. IV. Checking the Type of Vector: - direct built in functions is used . V. Calculating Square Root and Natural Log: - `sqrt(x) and log(x)` calculates the square root and natural logarithm (base e) of each element in the vector. VI. Conversion and Type Checking: - `as.character()` converts the vector to a character vector, and `as.integer()` converts it to an integer vector. - `is.character()` and `is.integer()` are used to check the resulting types. VII. Naming Vector Elements: - `names()` is used to assign names to the elements of the vector. VIII. Subsetting Vector by Names: - To subset a vector by names, you can use square brackets with the names of the elements you want to extract. Bhukya annamaiah Code: Result and conclusions: We have performed the required task of finding the length of vector, extracting the element from vector, Checking the Type of Vector, Naming Vector Elements. Bhukya annamaiah EXPERIMENT-2 Date- 06 |10|23 Aim: Working with rbind ,cbind in R Problem : Generate a vector of 20 elements starting with 2.5 by a jump of 0.3 : I. II. III. IV. Check the class and type of this vector. Convert this to complex vector and save with name z. Extract all the elements which are at the eleven places. Extract the first five elements. Thereafter bind them row wise and column wise; and store under the name row_mat. Perform the testing for matrix and vector? Theory: A vector can be created using an in-built function in R called c(). Elements must be comma-separated. seq() generates a sequence starting from 2.5, with a step of 0.3, and a length of 20. I. Checking Class and Type: class() is used to check the class of the vector. The class represents the type of object in R, and in this case, it will likely return "numeric." typeof() is used to check the internal type of the vector. This will return "double," which is the internal representation of real numbers in R. II. Converting to Complex Vector: as.complex() converts the numeric vector to a complex vector. III. Extracting Elements at the 11th Place: my_vector[11] extracts the element at the 11th position in the vector. IV. Extracting, Binding, and Testing Matrices and Vectors: Bhukya annamaiah my_vector[1:5] extracts the first five elements from the vector. rbind() ,cbind() is used to bind these elts into a matrix rowand columnwise. is.matrix() is used to check if the resulting objects are matrices. is.vector() is used to check if the original vector is still a vector.Understanding these concepts will help you manipulate and analyze data in R. Bhukya annamaiah Code: > # I. Generating the vector > x<- seq(from = 2.5, by = 0.3, length= 20);x [1] 2.5 2.8 3.1 3.4 3.7 4.0 4.3 4.6 4.9 5.2 5.5 5.8 6.1 6.4 6.7 7.0 7.3 7.6 7.9 8.2 > > # I. Check the class and type of this vector. > class_of_vector <- class(x);class_of_vector [1] "numeric" > type_of_vector <- typeof(x);type_of_vector [1] "double" > > # II. Convert this to a complex vector and save it with the name z. > z <- as.complex(x);z [1] 2.5+0i 2.8+0i 3.1+0i 3.4+0i 3.7+0i 4.0+0i 4.3+0i 4.6+0i 4.9+0i 5.2+0i 5.5+0i 5.8+0i 6.1+0i 6.4+0i 6.7+0i 7.0+0i 7.3+0i 7.6+0i 7.9+0i 8.2+0i > > # III. Extract all the elements which are at the eleven places. > elements_at_eleven <- x[11];elements_at_eleven [1] 5.5 > > # IV. Extract the first five elements, bind them row-wise and column-wise. > bind<- x[1:5] > row_mat <- rbind(bind,bind);row_mat [,1] [,2] [,3] [,4] [,5] bind 2.5 2.8 3.1 3.4 3.7 bind 2.5 2.8 3.1 3.4 3.7 > col_mat <- cbind(bind,bind);col_mat bind bind [1,] 2.5 2.5 [2,] 2.8 2.8 [3,] 3.1 3.1 [4,] 3.4 3.4 [5,] 3.7 3.7 > > # Perform testing for matrix and vector. > is.matrix(row_mat) # Check if it's a matrix (row-wise) [1] TRUE > is.matrix(col_mat) # Check if it's a matrix (column-wise) [1] TRUE > is.vector(x) # Check if the original vector is still a vector [1] TRUE Result: 1-Performed the following task of Checking the class and type of given vector. 2-Converted vector to complex vector and save with name z. 3-Extracted the required elements. Bhukya annamaiah EXPERIMENT-3 Date- 10 |10|23 Aim: Problem : Generate one vector of size=5 of 2’s and second vector of size m=4 of size 5’s and perform the following tasks: I. II. III. Combine them into a single vector. Add an element to the original vector, whose value is 1 and store them under the name value. Extract all the elements which are at the odd places. Theory: I. Generating the Vectors: rep(2, times = 5) creates a vector of size 5 filled with the value 2. rep(5, times = 4) creates a vector of size 4 filled with the value 5. I. Combining Vectors: c(vector_of_twos, vector_of_fives) is used to combine the two vectors into a single vector. The c() function concatenates vectors. II. Adding an Element: c(combined_vector, 1) adds the element with the value 1 to the combined vector. III. Extracting Elements at Odd Places: value[seq(1, length(value), by = 2)] extracts all elements that are at odd positions in the vector. In R, indexing starts from 1. Understanding these concepts will help you work with vectors and perform various operations in R. Bhukya annamaiah Code: Result: Combine them into a single vector. Add an element to the original vector, whose value is 1 and store them under the name value. Extract all the elements which are at the odd places. Bhukya annamaiah EXPERIMENT-4 Date-10 |10|23 Aim: Working with complex vectors Problem : Define a vector of the complex numbers, whose elements are 1+2i, 41i,2+1i,22i,5-1i,3+6i and perform the following tasks: I. Extract the real and imaginary part of the above vector. II. Calculate the modulus and complex conjugate of the above vector. III. Check the type and class of this vector, also use () function to get the full detail of this vector. Theory: I. Extracting Real and Imaginary Parts: Re(complex_vector) extracts the real parts of the complex numbers in the vector. Im(complex_vector) extracts the imaginary parts of the complex numbers in the vector. II. Calculating Modulus and Complex Conjugate: Mod(complex_vector) calculates the modulus (or absolute value) of each complex number in the vector. The modulus of a complex number a + bi is sqrt(a^2 + b^2). Conj(complex_vector) calculates the complex conjugate of each complex number in the vector. The complex conjugate of a complex number a + bi is a - bi. Bhukya annamaiah III. Checking Type, Class, and Details: typeof(complex_vector) returns the internal type of the vector. In this case, it will be "complex." class(complex_vector) returns the class of the vector. It will be "complex" or "numeric" since complex numbers are represented as "numeric" in R. str(complex_vector) provides detailed information about the vector, including its class, length, and elements. Code: Result: We have done the analysis with complex vector as asked in the question. Bhukya annamaiah EXPERIMENT-5 Date- 13|10|23 Aim: Working with transpose, inverse, row sums, column sums, row mean and column mean of the above matrix. Problem : Define the following matrix with name mat_A in R. 5.07 3.19 -2.32 1.87 1.21 8.95 -2.04 4.49 -3.32 -4.66 9.93 2.05 1.18 6.72 -3.66 8.77 II. Print the number of row, column and dimensions of the above matrix. III. Calculate the transpose, inverse, row sums, column sums, row mean and column mean of the above matrix. IV. Use R function to print the diagonal of mat_A. V. Extract the nd 2 column of mat_A. VI. Extract the 3rd row of mat_A. VII. Extract the 4th entry of the 3rd column of mat_A. VIII. IX. Define the row names as R1, R2, R3, R4. After step 7, define the column names also as C1, C2, C3, C4. Theory: I. Defining a Matrix: matrix() is used to define a matrix in R. The c() function is used to create a vector of elements, and nrow and byrow are used to specify the number of rows and whether the elements should be filled by row. Bhukya annamaiah II. Number of Rows, Columns, and Dimensions: nrow(mat_A) and ncol(mat_A) are used to find the number of rows and columns in the matrix. dim(mat_A) returns the dimensions as a vector of rows and columns. III. Matrix Operations: t(mat_A) calculates the transpose of the matrix. solve(mat_A) finds the inverse of the matrix. rowSums(), colSums(), rowMeans(), and colMeans() are used to calculate row sums, column sums, row means, and column means, respectively. IV. Diagonal Elements: diag(mat_A) extracts the diagonal elements of the matrix. V. Extracting Columns and Rows: mat_A[, 2] extracts the second column. VI. Extracting an Entry: mat_A[3, 4] extracts the fourth entry in the third row of the matrix. VII. Defining Row and Column Names: rownames(mat_A) and colnames(mat_A) are used to define row and column names for the matrix. Bhukya annamaiah Code: Bhukya annamaiah Result: We have performed the following task of printing the number of row, column and dimensions of the given matrix. And calculated the transpose, inverse, row sums, column sums, row mean and column mean of the above matrix . Bhukya annamaiah