Changing Variable Values with mutate()

The mutate() function from the dplyr package is a powerful tool for creating and modifying variables within a data frame.

1. Changing Values Based on a Condition

You can use mutate() along with ifelse() to change the values of a variable based on a condition.

R
# Change 'score' to 100 if 'age' is greater than 40
data <- data %>%
  mutate(score = ifelse(age > 40, 100, score))

# Display the updated 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 100

2. Using mutate() with Multiple Conditions

You can use case_when() for more complex conditional logic.

R
# Change 'score' based on multiple conditions
data <- data %>%
  mutate(
    score = case_when(
      age <= 30 ~ score + 10,
      age > 30 & age <= 40 ~ score + 5,
      age > 40 ~ score + 15
    )
  )

# Display the updated data frame
print(data)

Output:

  id    name age score
1 1 Ali 25 98
2 2 Boby 30 102
3 3 Charles 35 90
4 4 David 40 92
5 5 Eva 45 115

3. Modifying Multiple Variables

You can modify multiple variables within a single mutate() call.

R
# Change 'age' and 'score' simultaneously
data <- data %>%
  mutate(
    age = age + 1,
    score = score * 1.1
  )
# Display the updated data frame
print(data)

Output:

  id    name age score
1 1 Ali 26 107.8
2 2 Boby 31 112.2
3 3 Charles 36 99.0
4 4 David 41 101.2
5 5 Eva 46 126.5

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.

Similar Reads

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....

Changing Variable Values with mutate()

The mutate() function from the dplyr package is a powerful tool for creating and modifying variables within a data frame....

Using transmute() to Change and Drop Variables

If you want to change the values of variables and simultaneously drop others, you can use transmute(). This function works similarly to mutate() but only keeps the variables that are explicitly mentioned....

Using across() for Multiple Columns

The across() function allows you to apply the same transformation to multiple columns simultaneously....

Conclusion

The dplyr package in R offers a versatile and efficient way to change the value of variables within a data frame. Whether you need to modify single variables based on specific conditions, update multiple variables simultaneously, or apply transformations across multiple columns, dplyr provides intuitive functions such as mutate(), transmute(), case_when(), and across() to accomplish these tasks. Mastering these functions can significantly enhance your data manipulation capabilities, making your data analysis workflows more efficient and effective....

Contact Us