Replace Value of Data Frame Variable Using dplyr Package in R (Example)

 

In this R post you’ll learn how to use the dplyr package to change particular values in a data frame column.

The table of content is structured as follows:

Here’s how to do it!

 

Example Data & Packages

We use the following data as basement for this R tutorial:

data <- data.frame(x1 = c(1, 2, 3, 2, 1, 1, 2),    # Create example data
                   x2 = "XX",
                   x3 = 66)
data                                               # Print example data
#   x1 x2 x3
# 1  1 XX 66
# 2  2 XX 66
# 3  3 XX 66
# 4  2 XX 66
# 5  1 XX 66
# 6  1 XX 66
# 7  2 XX 66

The previous output of the RStudio console shows the structure of our example data frame: It has seven rows and three columns.

In this R programming language tutorial, we’ll also have to install and load the dplyr package:

install.packages("dplyr")                          # Install dplyr package
library("dplyr")                                   # Load dplyr package

Now, we can use the functions of the dplyr package to modify specific values in our data frame.

 

Example: Changing Certain Values in Variable Using mutate() & replace() Functions

In this example, I’ll show how to replace particular values in a data frame variable by using the mutate and replace functions in R.

More precisely, the following R code replaces each 2 in the column x1:

data_new <- data %>%                               # Replacing values
  mutate(x1 = replace(x1, x1 == 2, 99))
data_new                                           # Print updated data
#   x1 x2 x3
# 1  1 XX 66
# 2 99 XX 66
# 3  3 XX 66
# 4 99 XX 66
# 5  1 XX 66
# 6  1 XX 66
# 7 99 XX 66

As you can see in the previous output of the RStudio console, each 2 in the variable x1 was replaced by the value 99.

 

Video & Further Resources

Do you need further information on the R code of this article? Then you may watch the following video of my YouTube channel. In the video, I’m explaining the R programming syntax of this page.

 

 

In addition, you could have a look at some of the other articles on my homepage.

 

Summary: In this R tutorial you learned how to replace values using the dplyr package. Let me know in the comments section, if you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


8 Comments. Leave new

  • Hi again, looks like some of my text in the previous comment was deleted, I paste here my comment again:

    How would you apply, two replace cases, in a column based on rows from another column? For example, giving the next data frame

    data <- data.frame(x1 = c(1, 2, 3, 2, 3, 1, 2, 3, 3, 2),
    x2 = c(4, 36, 45, 22, 36, 1, 11, 12, 31, 22))
    x1 x2
    #1 1 4
    #2 2 36
    #3 3 45
    #4 2 22
    #5 3 36
    #6 1 1
    #7 2 11
    #8 3 12
    #9 3 31
    #10 2 22

    I want to apply 2 conditions:
    If X1 = 1 then replace its corresponding value in X2 by YES
    If X1 = 2 then replace its corresponding value in X2 by NO
    Leave all the other values unchanged, keep the original values in X2

    How would you replace multiple cases without repeating the "replace" function? I wanted to use case_when for this but I did not find a solution. I used the mutate option then, but it replace the values I wanted to leave untouched by blanks

    data %
    mutate(x2 = case_when(
    x1 %in% 1 ~ “YES”,
    x1 %in% 2 ~ “NO”))

    And what I obtained:
    x1 x2
    #1 1 YES
    #2 2 NO
    #3 3
    #4 2 NO
    #5 3
    #6 1 YES
    #7 2 NO
    #8 3
    #9 3
    #10 2 NO

    Thank you in advance

    Reply
  • George Dontas
    March 17, 2022 2:44 pm

    How could I replace those values WITHOUT creating a new data frame?

    Reply
  • How could I replace several values in one column?

    Reply
  • Your channel and this site are really helpful. Thanks a lot!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top