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 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 = c("gr1", "gr2", "gr1", "gr3", "gr2"))
data$char <- as.character(data$char)          # Convert to character
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.

 

The YouTube video will be added soon.

 

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.

 



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.

Menu
Top