Open In App

How to Replace specific values in column in R DataFrame ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In this article, we will see how to replace specific values in a column of DataFrame in R Programming Language. 

To replace specific values in a column in an R DataFrame, you can follow these general steps.

  1. Select the DataFrame: First, we need to access the DataFrame in which we want to replace values. we can do this by either loading our data into a DataFrame or creating one from scratch.
  2. Identify the condition: Determine the condition or criteria for selecting the rows in which we want to replace values in the specified column. we can use logical expressions, functions, or other methods to define our condition.

Method 1: Using Replace() function

replace() function in R Programming Language is used to replace the values in the specified string vector x with indices given in the 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.

Replace specific values in column Using Replace() function

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 the negative numbers with 0.

Example 2: Replace Multiple Values in Vector Using Replace() function

We can replace multiple values in a vector using the replace() function in R by specifying a vector of conditions and a corresponding vector of replacement values.

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)
 
# Create a data frame
df <- data.frame(Roll_No, Names, Marks)
 
# Define the conditions and replacement values
conditions <- c("Sita", "Riya")
replacement_values <- c("NewSita", "NewRiya")
 
# Use replace() to replace the names in the 'Names' column
df$Names <- replace(df$Names, df$Names %in% conditions, replacement_values)
 
df


Output:

  Roll_No   Names Marks
1 1 Suresh 15
2 2 NewSita 20
3 3 Anu 3
4 4 Manasa 11
5 5 NewRiya 14
6 6 Ramesh 16
7 7 Roopa 10
8 8 Neha 13

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:

  Roll_No  Names Marks
1 1 Suresh 15
2 2 Sita 20
3 3 Anu 3
4 4 Manasa 11
5 5 Riya 14
6 6 Ramesh 16
7 7 Roopa 10
8 8 Neha 13 

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

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:

  Roll_No  Names Marks
1 1 Suresh 15
2 2 Sita 25
3 3 Anu 3
4 4 Manasa 11
5 5 Riya 14
6 6 Ramesh 16
7 7 Roopa 10
8 8 Neha 13

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”

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:

  Roll_No  Names Marks
1 1 Suresh 15
2 2 Sita 20
3 3 Anu 3
4 4 Manasa 11
5 5 Riya 14
6 6 Vikas 16
7 7 Roopa 10
8 8 Neha 13

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”.



Last Updated : 24 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads