How to Replace specific values in column in R DataFrame ?
In this article, we will see how to replace specific values in a column of DataFrame in R Programming Language.
Method 1: Using Replace() function.
replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values.
Syntax: replace(list , position , replacement_value)
It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.
 "Original DF" Roll_No Names Marks Full_Marks 1 1 Suresh 15 20 2 2 Sita 20 10 3 3 Anu 3 44 4 4 Manasa -1 21 5 5 Riya 14 24 6 6 Ramesh -2 36 7 7 Roopa 10 20 8 8 Neha 13 13  "Replaced Value" 15 20 3 0 14 0 10 13
You can see in the above code we have replaced the 2nd element from “Sonam” to “Harish”.
Method 2: Using the logical condition.
Now let us see how we can replace values of specific values in the column using logical conditions. First, let us create the data frame in R
Now, let’s see how can we replace specific values in the column.
Example 1: In our data frame “Sita” marks are given as 20 let us replace it with 25.
- y:It is the value which help us to fetch the data location the column
- x: It is the value which needs to be replaced
In the above code, you can see that we have used two columns(Marks, Names) in our data frame “df”. First, we fetch the data that need to be replaced, In our case, we need to replace the marks of a person whose name is “Sita”. So, We go through the Marks column and stop Where the name connected to those marks is “Sita”. After we find that location we replace the marks with 25
Let us replace the name of “Ramesh” with “Vikas”.
In the above code you can see that we are fetching a row where name is “Ramesh”, So it is like a linear search in a list to find the location of a given element After we find that location we replace the name with “Vikas”.