Skip to content
Related Articles

Related Articles

Improve Article

How to Replace specific values in column in R DataFrame ?

  • Last Updated : 01 Jun, 2021

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.



Example:

R




# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, -1, 14, -2 , 10, 13)
Full_Marks<-c(20, 10, 44, 21, 24, 36, 20, 13)
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names, Marks, Full_Marks)
 
print("Original DF")
print(df)
 
print("Replaced Value")
 
#replaces the negative numbers with zeros
data<-replace(df$Marks, df$Marks<0, 0)
print(data)

Output:

[1] "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
[1] "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  

R




# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
print(df)

Output:
 



Data Frame

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.

Syntax: dataframe_name$column_name1[dataframe_name$column_name2==y]<-x

Parameters:

  • 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

Code:

R




# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names, Marks)
 
df$Marks[df$Names == "Sita"] <- 25
# Column_name1 is Marks which we need to replace
 
# Print the modified data frame
print(df)

Output:

Sita marks are 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

Example 2:

Let us replace the name of “Ramesh” with “Vikas”. 

R




# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
df$Names[df$Names=="Ramesh"]<-"Vikas"
 
# Prints the modified data frame
print(df)

Output:

Ramesh is replaced by 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”.




My Personal Notes arrow_drop_up
Recommended Articles
Page :