r dataframe column factor
# Step 1: Create a data frame
df <- data.frame(
Name = c("Alice", "Bob", "Charlie", "David"),
Age = c(25, 30, 22, 28),
Gender = c("Female", "Male", "Male", "Male")
)
# Step 2: Convert the 'Gender' column to a factor
df$Gender <- factor(df$Gender)
# Step 3: Display the data frame
print(df)
Explanation:
1. We create a data frame df
with columns 'Name', 'Age', and 'Gender'.
2. We convert the 'Gender' column to a factor using the factor()
function, which is useful for categorical variables.
3. Finally, we print the data frame to display the changes.