Replace values from dataframe column using R
Last Updated :
07 Apr, 2021
In this article, we will discuss how to replace values from a DataFrame column in R Programming Language.
Whenever we deal with some data, it is necessary that we draw accurate conclusions from it. Now imagine what if, there are some missing values(this happens when some observation is missing in a column of a data-frame or contains a character value instead of a numerical value) in our data or let's say there are some negative values in the dataset, which might lead to false accuracy, also it affects the analysis result. So, to avoid these, we can replace such values, using the following methods.
Example 1. Replacing NA values in a data frame with Zeroes(0's)
So first, we create a table with the column names: Name, ID, CPI and add respective values to the respective columns
R
Name <- c("Amy", "Celine", "Lily",
"Irene", "Rosy", "Tom", "Kite")
ID <- c(123, NA, 134, NA, 166, 129, 178)
CPI <- c(8.5, 8.3, 7.8, NA, 6.9, 9.1, 5.6)
details <- data.frame(Name, ID, CPI)
View(details)
Output:
Replace value from NA values to 0:
R
details[is.na(details)] <- 0
# views the newly modified table
View(details)
Output:
Example 2. Replacing NA values in a data frame with the mean values
We create the table with the following columns and values, same as explained before.
R
RollNo <- c(24,23,NA,18)
ID <- c(123, 156, 134, 148)
CPI <- c(8.5,8.3,7.8,NA)
secR1 <- data.frame(RollNo,ID,CPI)
View(secR1)
Output:
Table
Now replacing the NA value with the mean value:
R
ex <- secR1
ex
# replacing the NA value with the mean value
ex$RollNo[is.na(ex$RollNo)]<-mean(ex$RollNo, na.rm = T)
round(ex, digits = 0)
Output:
The Output --> NA in Roll no. replaced with the mean
Example 3. Replacing the negative values with 0s(zeroes):
First, we create a table, as we did before.
R
RollNo <- c(24,23,16,-18)
ID <- c(123,156,-134,148)
CPI <- c(8.5,8.3,7.8,8.9)
marks <- c(-54,70,-20,9)
secR1 <- data.frame(RollNo,ID,CPI,marks)
View(secR1)
Output:
Now change the negative values to 0:
R
secR1[secR1 < 0] <- 0
view(secR1)
Output:
Example 4. Using replace( ) method:
The replace() function, replaces the values in x with indices given in the list by those given in values. If necessary, the values in values are recycled.
Syntax: replace(x, list, values)
Arguments:
- x: vector
- list: an index vector
- values: replacement values
Example:
R
# creates a table
RollNo <- c(24,23,NA,18)
ID <- c(123, 156, 134, 148)
CPI <- c(8.5,8.3,7.8,NA)
secR1 <- data.frame(RollNo,ID,CPI)
View(secR1)
# 2nd element of the list, hence 2, replacement value is 45
secR1<- replace(RollNo, 2, 45)
View(secR1)
Output:

Final Output
Similar Reads
Replace Spaces in Column Names in R DataFrame In this article, we will replace spaces in column names of a dataframe in R Programming Language. Let's create a Dataframe with 4 columns with 3 rows: R # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql","oracle","mong
2 min read
Replace specific values in column using regex in R In this article, we will discuss how to replace specific values in columns of dataframe in R Programming Language. Method 1 : Using sub() method The sub() method in R programming language is a replacement method used to replace any occurrence of a pattern matched with another string. It is operative
3 min read
Select DataFrame Column Using Character Vector in R In this article, we will discuss how to select dataframe columns using character vectors in R programming language. Data frame in use: To extract columns using character we have to use colnames() function and the index of the column to select is given with it using []. The approach is sort of the sa
2 min read
Select Only Numeric Columns from DataFrame in R In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric colu
2 min read
Replace contents of factor column in R dataframe In this article, we are going to see how to replace the content of the factor column in the dataframe in R Programming Language. Example 1: Replacing content of factor column Initially, the factor column is converted to a character column using the explicit conversion of as.character() method in R.
2 min read
Split DataFrame Variable into Multiple Columns in R In this article, we will discuss how to split dataframe variables into multiple columns using R programming language. Method 1: Using do.call method The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into
3 min read