1. Introduction
This R program creates a data frame from four given vectors. Each vector becomes a column in the resulting data frame.
2. Program Steps
1. Create four individual vectors.
2. Combine these vectors into a data frame.
3. Print the resulting data frame.
3. Code Program
# Step 1: Create four vectors
vector1 <- c(1, 2, 3, 4)
vector2 <- c("a", "b", "c", "d")
vector3 <- c(TRUE, FALSE, TRUE, FALSE)
vector4 <- c(1.1, 2.2, 3.3, 4.4)
# Step 2: Combine the vectors into a data frame
data_frame <- data.frame(vector1, vector2, vector3, vector4)
# Step 3: Print the data frame
print("Data Frame created from vectors:")
print(data_frame)
Output:
Data Frame created from vectors: vector1 vector2 vector3 vector4 1 1 a TRUE 1.1 2 2 b FALSE 2.2 3 3 c TRUE 3.3 4 4 d FALSE 4.4
Explanation:
1. c(1, 2, 3, 4), c("a", "b", "c", "d"), c(TRUE, FALSE, TRUE, FALSE), c(1.1, 2.2, 3.3, 4.4): Create four vectors with different data types.
2. data.frame(vector1, vector2, vector3, vector4): Combines the four vectors into a data frame. Each vector becomes a column.
3. print("Data Frame created from vectors:"): Prints a message indicating the data frame created from the vectors.
4. print(data_frame): Displays the content of the data frame.
Comments
Post a Comment