Replace Particular Value in Data Frame in R (2 Examples)
This tutorial explains how to change particular values in a data frame to different values in the R programming language.
Table of contents:
- Introduction of Example Data
- Example 1: Replace Character or Numeric Values in Data Frame
- Example 2: Replace Factor Values in Data Frame
- Video & Further Resources
Let’s take a look at some R codes in action…
Introduction of Example Data
The examples of this R programming tutorial are based on the following example data frame in R:
data <- data.frame(x1 = 1:5, x2 = LETTERS[1:5], x3 = c("A", "C", "A", "A", "B"), x4 = factor(c("f1", "f2", "f3", "f2", "f1")), stringsAsFactors = FALSE) data # x1 x2 x3 x4 # 1 1 A A f1 # 2 2 B C f2 # 3 3 C A f3 # 4 4 D A f2 # 5 5 E B f1
Our example data consists of five rows and four variables. The first column is numeric, the second and third columns are characters, and the fourth column is a factor.
Example 1: Replace Character or Numeric Values in Data Frame
Let’s first replicate our original data in a new data object:
data1 <- data # Replicate data
Now, let’s assume that we want to change every character value “A” to the character string “XXX”. Then we can apply the following R code:
data1[data1 == "A"] <- "XXX" data1 # x1 x2 x3 x4 # 1 1 XXX XXX f1 # 2 2 B C f2 # 3 3 C XXX f3 # 4 4 D XXX f2 # 5 5 E B f1
As you can see based on the output of the RStudio console, each “A” in the variables x2 and x3 was replaced by “XXX”.
Note that we could apply exactly the same code to replace numeric values (such as the numbers in column x1). Furthermore, we could replace a value by NA instead of a character.
However, with factors it gets a bit more complicated…
Example 2: Replace Factor Values in Data Frame
Again, we are replicating our original data first:
data2 <- data # Replicate data
Now, let’s try to apply the same type of R syntax as in Example 1 to our factor column x4:
data2[data2 == "f2"] <- "YYY" # Warning: # In `[<-.factor`(`*tmp*`, thisvar, value = "YYY") : # invalid factor level, NA generated
As you can see, R returns a warning message: invalid factor level, NA generated.
Let’s have a look how our new data frame looks like:
data2 # x1 x2 x3 x4 # 1 1 A A f1 # 2 2 B C <NA> # 3 3 C A f3 # 4 4 D A <NA> # 5 5 E B f1
Oh gosh! Every element with the factor level f1 was replaced by NA. Definitely not what we wanted.
Let’s start all over with the replication of our example data:
data2 <- data # Replicate data
If we want to convert a factor value in a data frame to a different value, we have to convert the factor to the character class first:
data2$x4 <- as.character(data2$x4)
Now, we can apply the same R code as in Example 1:
data2[data2 == "f2"] <- "YYY"
Afterwards, we can convert our character back to the factor class:
data2$x4 <- as.factor(data2$x4) data2 # x1 x2 x3 x4 # 1 1 A A f1 # 2 2 B C YYY # 3 3 C A f3 # 4 4 D A YYY # 5 5 E B f1
Video & Further Resources
Do you need further info on the R codes of this post? Then I can recommend to watch the following video of my YouTube channel. In the video, I illustrate the R programming syntax of this page:
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.
Furthermore, I can recommend to have a look at the other R programming articles of my website. A selection of articles is listed here:
- Replace 0 with NA
- R Replace NA with 0
- replace Function in R
- Convert Character to Factor in R
- The R Programming Language
In this R tutorial you learned how to find and exchange specific values in multiple columns of a data matrix. Let me know in the comments section, if you have additional questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on new articles.
Statistics Globe Newsletter