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 |
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 |
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 |
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 |
# 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 |
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 |
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 |
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 |
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 |
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might have a look at the related articles of my homepage.
- Convert Character to Factor in R
- Replace Particular Value in Data Frame
- Replace Multiple Letters with Accents
- Replace Inf with NA in Vector & Data Frame
- Replace 0 with NA in R
- replace Function in R
- The R Programming Language
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.
Statistics Globe Newsletter
12 Comments. Leave new
Thanks to your detailed comment : 3) Example 2: Conditionally Exchange Values in Character Variable,
I could render the dataset.
there was a mistake: Example 2: Conditionally Exchange Values in Character Variable
Hey Lynn,
I just saw your second comment. Example 2 works fine for me as well though. Could you share your code?
Regards
Joachim
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
Joachim, I think what you are showing is how to replace values, but not conditional exchanges, as there is no condition here.
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
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.
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:
Regarding your second question, you may use the following code (note the ! operator at the beginning of the logical condition):
Regards,
Joachim
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
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
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
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