Write a R program to create three vectors a,b,c with 3 integers. Combine the three vectors to become a 3×3 matrix where each column represents a vector. Print the content of the matrix

1. Introduction

This R program creates three individual vectors, each containing three integers, and then combines these vectors into a 3x3 matrix. Each column of the matrix represents one of the vectors.

2. Program Steps

1. Create three vectors, a, b, and c, each containing three integers.

2. Combine these vectors into a 3x3 matrix.

3. Print the content of the resulting matrix.

3. Code Program

# Step 1: Create three vectors with three integers each
a <- c(1, 2, 3)  # First vector
b <- c(4, 5, 6)  # Second vector
c <- c(7, 8, 9)  # Third vector

# Step 2: Combine the vectors to form a 3x3 matrix
matrix_3x3 <- cbind(a, b, c)

# Step 3: Print the matrix
print("3x3 Matrix:")
print(matrix_3x3)

Output:

3x3 Matrix:
     a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Explanation:

1. a <- c(1, 2, 3): Initializes the first vector a with integers 1, 2, and 3.

2. b <- c(4, 5, 6): Initializes the second vector b with integers 4, 5, and 6.

3. c <- c(7, 8, 9): Initializes the third vector c with integers 7, 8, and 9.

4. cbind(a, b, c): Combines the vectors a, b, and c into a 3x3 matrix. Each vector becomes a column in the matrix.

5. print("3x3 Matrix:"): Prints a message indicating that the following output is a 3x3 matrix.

6. print(matrix_3x3): Displays the content of the 3x3 matrix.


Comments