Change the value of a variable with dplyr
dplyr is part of the tidyverse, a collection of R packages designed for data science. The key functions of dplyr that are commonly used include select(), filter(), mutate(), summarise(), and arrange(). Among these, mutate() is the primary function for modifying or creating new variables in a data frame.
Before diving into the examples, let’s create a sample data frame to work with:
# Load dplyr package
library(dplyr)
# Create a sample data frame
data <- data.frame(
id = 1:5,
name = c("Ali", "Boby", "Charles", "David", "Eva"),
age = c(25, 30, 35, 40, 45),
score = c(88, 92, 85, 87, 90)
)
# Display the data frame
print(data)
Output:
id name age score
1 1 Ali 25 88
2 2 Boby 30 92
3 3 Charles 35 85
4 4 David 40 87
5 5 Eva 45 90
How to Change value of variable with dplyr
The Dplyr package in R is a powerful tool for data manipulation and transformation. It provides a set of functions that allow you to perform common data manipulation tasks concisely and efficiently. One of these tasks is changing the value of a variable within a data frame. This article will guide you through various methods to change the value of a variable using dplyr.
Contact Us