Replace Values in Data Frame Conditionally in R (4 Examples)

 

In this tutorial, I’ll show how to exchange specific values in the columns of a data frame based on a logical condition in R.

The content of the article looks like this:

So without further ado, here’s how to do it!

 

Creation of Example Data

As a first step, we’ll have to create some data that we can use in the examples below:

data <- data.frame(num1 = 1:5,                # Example data
                   num2 = 3:7,
                   char = letters[1:5],
                   fac = as.factor(c("gr1", "gr2", "gr1", "gr3", "gr2")))
data                                          # Print example data
#   num1 num2 char fac
# 1    1    3    a gr1
# 2    2    4    b gr2
# 3    3    5    c gr1
# 4    4    6    d gr3
# 5    5    7    e gr2

Have a look at the previous RStudio console output. It shows that the example data contains of four columns. Two of the variables are numeric, one of the variables is a character, and another one of the variables has the factor class.

Let’s exchange some of the values in our data conditionally!

 

Example 1: Conditionally Exchange Values in Numeric Variable

The following R programming syntax illustrates how to perform a conditional replacement of numeric values in a data frame variable. Have a look at the following R code:

data$num1[data$num1 == 1] <- 99               # Replace 1 by 99
data                                          # Print updated data
#   num1 num2 char fac
# 1   99    3    a gr1
# 2    2    4    b gr2
# 3    3    5    c gr1
# 4    4    6    d gr3
# 5    5    7    e gr2

As you can see based on the previous output, we have replaced the value 1 by the value 99 in the first column of our data frame.

 

Example 2: Conditionally Exchange Values in Character Variable

This Example illustrates how to insert new values in character variables. The syntax is basically the same as in Example 1. However, this time the values should be wrapped with quotation marks:

data$char[data$char == "b"] <- "XXX"          # Replace b by XXX
data                                          # Print updated data
#   num1 num2 char fac
# 1   99    3    a gr1
# 2    2    4  XXX gr2
# 3    3    5    c gr1
# 4    4    6    d gr3
# 5    5    7    e gr2

The previous R code replaced the character “b” with the character string “XXX”.

 

Example 3: Conditionally Exchange Values in Factor Variable

Example 3 shows how to replace factor levels. The exchange of values in factors is slightly more complicated as in case of numeric or character vectors. If you would use the code shown in Examples 1 and 2, the following Warning would be shown:

# Warning:
# In `[<-.factor`(`*tmp*`, data$fac == "gr1", value = c(NA, 2L, NA,  :
#   invalid factor level, NA generated

The reason is that factor variables have fixed factor levels. When we insert new values to our factor, the factor variable cannot assign the values to an existing factor level and for that reason NA values (i.e. missing values) are generated.

For that reason, we have to convert our factor column to the character data type first:

data$fac <- as.character(data$fac)            # Convert factor to character

Now, we can apply the same code as in Example 2:

data$fac[data$fac == "gr1"] <- "new_group"    # Replace gr1 by new_group

And finally, we can convert the character variable back to the factor class:

data$fac <- as.factor(data$fac)               # Convert character to factor

Let’s have a look at the new data:

data                                          # Print updated data
#   num1 num2 char       fac
# 1   99    3    a new_group
# 2    2    4  XXX       gr2
# 3    3    5    c new_group
# 4    4    6    d       gr3
# 5    5    7    e       gr2

As you can see, the factor level gr2 was replaced by the new factor level new_group.

 

Example 4: Conditionally Exchange All Values in Whole Data Frame

It is also possible to replace a certain value in all variables of a data frame. The following R code shows how to do that:

data[data == 3] <- 777                        # Replace all values
data                                          # Print updated data
#   num1 num2 char       fac
# 1   99  777    a new_group
# 2    2    4  XXX       gr2
# 3  777    5    c new_group
# 4    4    6    d       gr3
# 5    5    7    e       gr2

We just replaced the value 3 by 777 in all columns of our data matrix.

 

Video & Further Resources

I have recently published a video on my YouTube channel, which explains the R syntax of this tutorial. Please find the video below.

 

 

In addition, you might have a look at the related articles of my homepage.

 

In this R tutorial you learned how to replace certain data frame values. Please let me know in the comments section, if you have any additional questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on the newest tutorials.

 

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.


12 Comments. Leave new

  • Thanks to your detailed comment : 3) Example 2: Conditionally Exchange Values in Character Variable,
    I could render the dataset.

    Reply
    • there was a mistake: Example 2: Conditionally Exchange Values in Character Variable

      Reply
    • Hey Lynn,

      Thank you for your comment! For me, the example works fine. Could you share the code you have used? Did R return an error or warning message?

      Regards

      Joachim

      Reply
  • Sebastián Peña
    June 24, 2021 1:31 pm

    Joachim, I think what you are showing is how to replace values, but not conditional exchanges, as there is no condition here.

    Reply
    • Hey Sebastián,

      Thank you for the comment!

      There is a logical condition though. For instance, the logical condition of Example 1 is data$num1 == 1.

      Regards

      Joachim

      Reply
  • Marina Bartolomé
    January 14, 2022 12:22 pm

    Dear Joachim, thank you for the information you give in your webpage, it is very clear and useful.

    I would like to know how to replace multiple values in the same column. I have found different options but they seem to me unnecessary complex.

    Is there any way I can replace different values using this function:

    data$fac[data$fac == “gr1”] <- "new_group"

    I tried "this" (with my own data and different names of course) but it did not work:

    data$fac[data$fac == "gr1", "gr2", "gr3"] <- "new_group"

    Also, I have another question related to that. I want to change multiple variables at the same time of the same column under a condition.

    In my case, I have a variable with multiple categories. I want to stay with 4 categories and group all the other responses into a category called "other".

    Do you have any advice on what function should I use? Again, I found some examples but I did not manage to run them correctly.

    Reply
    • Hey Marina,

      Thank you very much for the kind feedback, glad you like my tutorials!

      Yes, you may use the %in% operator to replace multiple values:

      data$fac[data$fac %in% c("gr1", "gr2", "gr3")] <- "new_group"

      Regarding your second question, you may use the following code (note the ! operator at the beginning of the logical condition):

      data$fac[! data$fac %in% c("gr1", "gr2", "gr3")] <- "other"

      Regards,
      Joachim

      Reply
  • Hi,

    I came here from your amazing you tube Videos.

    Can you help with below two:

    1) R to replace blank column with another column data
    2) R to open text file and to do vlookup only on the certain blank values in a column and save it

    Reply
    • Hey Aditya,

      Thank you very much for the kind feedback, glad you like my video tutorials!

      Regarding 1) Please try the following code:

      data$blank_column <- data$non_blank_column

      Regarding 2) You may have a look here for more info on how to read text files.

      I hope that helps!

      Joachim

      Reply
  • Hi Joachim,

    Thankyou for all your help in R!

    I am stuck on this problem – I have two data frames. I want to update the values in 3 columns from one data frame to the other, BUT the replacement of these 3 columns depends on matching “cells” in an ID column.

    Its sort of like: IF a value in this ID column of DataFrame A, matches a value in ID column of DataFrame B, then replace certain values in that row with the values of this row.

    I hope this makes sense!

    Thankyou

    Reply
    • Hey Susie,

      Thank you very much for the kind comment, glad you like the article!

      Please excuse the delayed response. I was on a long holiday, so unfortunately I wasn’t able to reply sooner. Still need help with your code?

      Regards,
      Joachim

      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