Open In App

How to Replace particular value in R dataframe ?

Improve
Improve
Like Article
Like
Save
Share
Report

Often, some values in our dataframe are not appropriate, they are not up-to-date, or we aren’t aware of those values. In such cases, we replace those values, because they are causing ambiguity. Over here, we will use the term NA, which stands for Non-Available to replace the unknown values. In this article, we will see how to change or replace all particular values in a table, with the help of the R programming language. 

For doing this there needs to be a condition on the basis on which the replacement has to be performed. The data in consideration is checked against this condition and if falls to be True is replaced by a particular value. This value can be another value of the datatype of the deleted value or NA. Let us look at various implementations that depict the same. We will discuss that a specific value can be replaced both in a particular column and the complete dataframe.

Dataframe in use:

StudentDetails

Replacing value for an individual column

For this first of the column on which the changes are to be made is selected and then the condition is applied to it. The cells where the condition falls True are replaced accordingly.

Example:

R




# creates table with column names 
# and values assigned to them
Name <- c('Adrian', 'Nathan','Heather',
          'Abby', 'Delight', 'Hope'
          'Lucifer', 'Faith',14,'Joseph')
  
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
  
ID <- c(123, 336, 134, 148, 14, 289, 856, 
        773, 201, 536)
  
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3, 7.11,
         7.9, 14.0)
  
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23, 14, 52, 10)
  
  
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
  
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
  
#values equal to 14 are replaced 
# with NA
sdetails$HostelRoom[sdetails$HostelRoom == 14] <- NA
  
# views the modified table
View(sdetails)


Output:

After modification: details

Multiple conditions can also be specified for the purpose of replacement.

Example:

R




# creates table with column names 
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
          'Abby','Delight','Hope',
          'Lucifer','Faith',14,'Joseph')
  
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
  
ID <- c(123, 336, 134, 148, 14, 289, 856, 
        773, 201, 536)
  
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3, 
         7.11, 7.9, 14.0)
  
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23, 
                14, 52, 10)
  
# stores it in the form of data frame 
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
  
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
  
# values less than or equal to 20, 
# will be replaced by NA
sdetails$RollNo[sdetails$RollNo<=20] <- "NA"
  
# views the modified table
View(sdetails)


Output:

After modification: details

An existing value can also be replaced by a specific value of the same datatype.

Example:

R




# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
          'Abby','Delight','Hope',
          'Lucifer','Faith',14,'Joseph')
  
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
  
ID <- c(123, 336, 134, 148, 14, 289, 856, 
        773, 201, 536)
  
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3, 
         7.11, 7.9, 14.0)
  
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23, 
                14, 52, 10)
  
# stores it in the form of data frame 
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
  
# saves the original dataframe with 
# the name sdetails
sdetails <- StudentDetails
   
# Under CPI named column, 8.30 is replaced 
# with 9.00 and 14.00 is replaced with 0.00
sdetails$CPI[sdetails$CPI == 8.30] <- 9.00
sdetails$CPI[sdetails$CPI == 14.00] <- 0.00
  
# views the modified table
View(sdetails)


Output:

Values after replacement

Replacing values for an entire dataframe

Now, we will be operating on replacing the values for the entire dataframe, irrespective of the columns. For this, all values in the dataframe are checked against the condition and the values that turn out to be True are replaced accordingly.

Program:

R




# creates table with column names 
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
          'Abby','Delight','Hope',
          'Lucifer','Faith',14,'Joseph')
  
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
  
ID <- c(123, 336, 134, 148, 14, 289, 856, 
        773, 201, 536)
  
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3, 
         7.11, 7.9, 14.0)
  
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23, 
                14, 52, 10)
  
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
  
# saves the original dataframe with 
# the name sdetails
sdetails <- StudentDetails
  
# replace the values numbered as 14, 
# with NA in the entire dataframe
sdetails[sdetails == 14] <- NA
  
# views the modified table
View(sdetails)


Output:

Values after replacement



Last Updated : 08 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads