R
R
# 1. Creating vectors
vector1 <- c(1, 2, 3, 4, 5) # Numeric vector
vector2 <- c("a", "b", "c", "d") # Character vector
vector3 <- c(TRUE, FALSE, TRUE) # Logical vector
# Display vectors
print("Numeric vector:")
print(vector1)
print("Character vector:")
print(vector2)
print("Logical vector:")
print(vector3)
# 2. Vector arithmetic
vector4 <- c(10, 20, 30, 40, 50)
sum_vector <- vector1 + vector4
diff_vector <- vector4 - vector1
prod_vector <- vector1 * vector4
quot_vector <- vector4 / vector1
print("Sum of vectors:")
print(sum_vector)
print("Difference of vectors:")
print(diff_vector)
print("Product of vectors:")
print(prod_vector)
print("Quotient of vectors:")
print(quot_vector)
# 3. Vector functions
mean_vector1 <- mean(vector1)
sum_vector1 <- sum(vector1)
length_vector1 <- length(vector1)
print("Mean of vector1:")
print(mean_vector1)
print("Sum of vector1:")
print(sum_vector1)
print("Length of vector1:")
print(length_vector1)
# 4. Indexing vectors
second_element <- vector1[2]
elements_2_to_4 <- vector1[2:4]
# 6. Combining vectors
combined_vector <- c(vector1, vector4)
print("Combined vector of vector1 and vector4:")
print(combined_vector)
print("Repetition vector:")
print(rep_vector)
print("Sequence vector:")
print(seq_vector)
Explanation:
1. Creating vectors: This shows how to create numeric, character, and logical vectors using
the c() function.
2. Vector arithmetic: Demonstrates element-wise addition, subtraction, multiplication, and
division.
3. Vector functions: Uses functions like mean(), sum(), and length() to get summary
statistics.
4. Indexing vectors: Accesses elements of a vector using indices.
5. Logical operations on vectors: Creates a logical vector based on a condition and filters
the vector.
6. Combining vectors: Combines two vectors using the c() function.
7. Repetition and sequences: Creates vectors using rep() and seq() functions.
4o
AIM
ChatGPT
The aim of this R script is to introduce and demonstrate the fundamental concepts and operations
associated with vectors in R. By running this script, users will learn how to:
Overall, the script serves as an educational tool to help users understand the core concepts of
vectors in R and how to work with them effectively.
4o
ALGLORITHM INPUT OUTPUT RESULT
ChatGPT
Sure! Let's break down the script into an algorithm, including the inputs, outputs, and expected
results.
Algorithm
1. Start
2. Create Vectors
o Input: Define numeric, character, and logical vectors.
o Output: Display the created vectors.
3. Perform Vector Arithmetic
o Input: Two numeric vectors.
o Process: Calculate sum, difference, product, and quotient of the vectors.
o Output: Display results of arithmetic operations.
4. Use Vector Functions
o Input: A numeric vector.
o Process: Calculate mean, sum, and length of the vector.
o Output: Display the results of these functions.
5. Index Vectors
o Input: A numeric vector.
o Process: Access specific elements and ranges of elements.
o Output: Display the accessed elements.
6. Apply Logical Operations
o Input: A numeric vector.
o Process: Create a logical vector based on a condition, and filter the original vector
using this logical vector.
o Output: Display the logical vector and the filtered vector.
7. Combine Vectors
o Input: Two numeric vectors.
o Process: Combine them into a single vector.
o Output: Display the combined vector.
8. Generate Vectors using Repetition and Sequences
o Input: Values to generate repetition and sequence vectors.
o Process: Create repetition and sequence vectors.
o Output: Display the repetition and sequence vectors.
9. End
Input
1. Vectors to be created:
o Numeric vector: c(1, 2, 3, 4, 5)
Output
1. Created Vectors:
o Numeric vector: [1, 2, 3, 4, 5]
Result
By following this script, users will understand how to create and manipulate vectors in R,
perform basic arithmetic and logical operations, access specific elements, and generate vectors
using repetition and sequences. This foundational knowledge will enable users to handle more
complex data structures and operations in R effectively.
# Display results
cat("Matrix 1:\n")
print(matrix1)
cat("Matrix 2:\n")
print(matrix2)
cat("Matrix Addition (Matrix 1 + Matrix 2):\n")
print(matrix_addition)
cat("Matrix Subtraction (Matrix 1 - Matrix 2):\n")
print(matrix_subtraction)
Input
Matrix 1:
o Number of rows: 2
o Number of columns: 2
o Elements: 1 2 3 4
Matrix 2:
o Number of rows: 2
o Number of columns: 2
o Elements: 5 6 7 8
Output
Matrix 1:
1 2
3 4
Matrix 2:
5 6
7 8
Matrix Addition (Matrix 1 + Matrix 2):
6 8
10 12
Matrix Subtraction (Matrix 1 - Matrix 2):
diff
-4 -4
-4 -4
Algorithm
1. Start
2. Define a function to get matrix input from the user:
o Prompt for the number of rows.
o Prompt for the number of columns.
o Prompt for the matrix elements row-wise.
o Validate the number of elements against the specified dimensions.
o Return the constructed matrix.
3. Get matrix inputs from the user:
o Call the input function to get the first matrix.
o Call the input function to get the second matrix.
4. Check if the dimensions of the two matrices match:
o If dimensions do not match, display an error message and stop execution.
5. Perform matrix addition:
o Add the two matrices element-wise.
6. Perform matrix subtraction:
o Subtract the second matrix from the first matrix element-wise.
7. Display the results:
o Print the original matrices.
o Print the result of the matrix addition.
o Print the result of the matrix subtraction.
8. End
Result
By running this script, users will be able to perform matrix addition and subtraction with
matrices they input, thereby understanding how to handle matrix operations in R.