Adding New Variables Using Base R
In base R, you can add new variables to a data frame by assigning a new column name to a vector of values. This vector can be the result of a transformation of existing columns or can be independently created.
1. Adding a Variable Directly
You can add a new variable directly to the data frame by creating a new column and assigning values to it.
# Add a new variable 'pass' based on 'score'
data$pass <- ifelse(data$score >= 90, "Yes", "No")
# Display the updated data frame
print(data)
Output:
id name age score pass
1 1 Ali 25 88 No
2 2 Boby 30 92 Yes
3 3 Charlie 35 85 No
4 4 David 40 87 No
5 5 Eva 45 90 Yes
2. Using transform()
The transform() function can also be used to add new variables to a data frame.
# Add a new variable 'age_group' using transform
data <- transform(data, age_group = ifelse(age < 35, "Young", "Old"))
# Display the updated data frame
print(data)
Output:
id name age score pass age_group
1 1 Ali 25 88 No Young
2 2 Boby 30 92 Yes Young
3 3 Charlie 35 85 No Old
4 4 David 40 87 No Old
5 5 Eva 45 90 Yes Old
How to Add Variables to a Data Frame in R
In data analysis, it is often necessary to create new variables based on existing data. These new variables can provide additional insights, support further analysis, and improve the overall understanding of the dataset. R, a powerful tool for statistical computing and graphics, offers various methods for computing and adding new variables to a data frame. This article will guide you through different approaches to achieve this in R, using built-in functions as well as packages like dplyr.
Contact Us