Replace NAs with specified values
Last Updated :
15 Mar, 2024
In this article, we will examine various methods to replace NAs with specified values in the R programming language.
What is NA?
NA stands for Not Available. These NAs are found with empty spaces or with a symbol NA which represents unavailable data points in the datasets. At the time of transferring a large amount of the data, can find NAs or missing values. These NAs are a very common issue in data preprocessing. To deal with NAs R provides some in-built functions such as an. omit(), complete.cases(), drop_na(), can avoid the NAs.
How do we replace NAs?
Replace NAs indicates, assigning the NAs with specific data such as integers, strings, float, and other types of data. So, these NAs can have the ability to accept data of any type. Replacing NAs with specific values is a crucial step in data preprocessing. Some of the methods to replace NAs are:
- is.na()
- replace()
Replacing the NAs by using is.na()
The function 'is.na()' is an in-built function which was provided by the R for replacing the NAs with specific values efficiently. In the function is.na(), "is" and "an" are the reserved words. The function 'is.na()' can able to works with dataframes, vectors, lists and matrices.
is.na(data)
In the below example, we created a dataframe by using the 'data.frame()'. After, By using the function 'df[is.na()]', we replaced NAs with a character 'g'.
R
print("The dataframe is")
#vector1
a=c("a",NA,"c","b")
#vector2
b=c(NA,"c",NA,"p")
#vector3
c=c("s","d","t",NA)
#creating dataframe
df=data.frame(a,b,c)
print(df)
print("Replacing the NAs")
df[is.na(df)]="g"
print(df)
Output:
a b c
1 a <NA> s
2 <NA> c d
3 c <NA> t
4 b p <NA>
[1] "Replacing the NAs"
a b c
1 a g s
2 g c d
3 c g t
4 b p g
In the below example, we created a matrix using the function 'matrix()'. After we are replaced the NAs with a interger 5.
R
#declaring the matrix
matrix=matrix(c(9,8,NA,6,NA,4,NA,2,1),nrow=3)
print(matrix)
#replacement value
replacement_value=5
#Assigning the NAs with 5
matrix[is.na(matrix)]=replacement_value
print(matrix)
Output:
[,1] [,2] [,3]
[1,] 9 6 NA
[2,] 8 NA 2
[3,] NA 4 1
Assigning the NAs with 5
[,1] [,2] [,3]
[1,] 9 6 5
[2,] 8 5 2
[3,] 5 4 1
In the below example, we created a vector. After, by using the function 'c[is.na()]', we are replaced the NAs with a integer 2 .
R
print("Before changing the vector")
#vector
c=c(NA,4,6,NA)
print(c)
#printing the vector
print("After changing the vector")
c[is.na(c)]=2
print(c)
Output:
[1] "Before changing the vector"
[1] NA 4 6 NA
[1] "After changing the vector"
[1] 2 4 6 2
Using the function replace()
The function 'replace()' is an in-built function which was provided by the R for replacing the NAs with specific values efficiently. It is similar to the function 'is.na()' and can able to works with dataframes, vectors, lists and matrices.
replace( data,condition,value)
In the below example, we created a matrix by using the function 'matrix()'. After, using the function 'replace()', we are replaced the NAs with a interger 8.
R
#declaring the matrix
matrix=matrix(c(9,8,NA,6,NA,4,NA,2,1),nrow=3)
print(matrix)
#replacement value
replacement_value=8
#Assigning the NAs with 8
matrix <- replace(matrix, is.na(matrix), replacement_value)
print(matrix)
Output:
[,1] [,2] [,3]
[1,] 9 6 NA
[2,] 8 NA 2
[3,] NA 4 1
[,1] [,2] [,3]
[1,] 9 6 8
[2,] 8 8 2
[3,] 8 4 1
In the below example, we created a dataframe by using the 'data.frame()' . After, using the function 'replace()', we replaced the NAs with a character 'g'.
R
print("The dataframe is")
#vector1
a1=c("a","b",NA,"c")
#vector2
b1=c(NA,9,NA,1)
#vector3
c1=c("g","f","g",NA)
#creating dataframe
df=data.frame(a1,b1,c1)
#printing
print(df)
print("Replacing the NAs")
res=replace(df,is.na(df),8)
print(res)
Output:
[1] "The dataframe is"
a1 b1 c1
1 a NA g
2 b 9 f
3 <NA> NA g
4 c 1 <NA>
[1] "Replacing the NAs"
a1 b1 c1
1 a 8 g
2 b 9 f
3 8 8 g
4 c 1 8
In the below example, we created a vector. Using the function 'replace()', we replaced the NAs with a interger 8.
R
print("Before changing the vector")
#vector
c=c(NA,4,6,NA)
print(c)
#printing the vector
print("After replacing the NAs in vector ")
res=replace(c,is.na(c),8)
print(res)
Output:
[1] "Before changing the vector"
[1] NA 4 6 NA
[1] "After replacing the NAs in vector "
[1] 8 4 6 8
Conclusion
In this article , we learned two different methods to replace NAs by using the functions is.na() and replace(). So R language provides many in-built functions to replace NAs with specific values.
Similar Reads
SQL Server REPLACE() Function
SQL Server is a strong relational database management system (RDBMS) developed to manage large data efficiently. In SQL Server, the REPLACE() function is used to modify or replace a substring within a given string. Taking about the real word uses, the REPLACE() function is vastly used in data proces
4 min read
How to replace a portion of strings with another value in JavaScript ?
In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca
3 min read
Replace all the NaN values with Zero's in a column of a Pandas dataframe
Replacing the NaN or the null values in  a dataframe can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method. We will discuss these methods along with an example demonstrating how to use it.                            DataFrame.fillna()
3 min read
Pandas Replace Multiple Values in Python
Replacing multiple values in a Pandas DataFrame or Series is a common operation in data manipulation tasks. Pandas provides several versatile methods for achieving this, allowing you to seamlessly replace specific values with desired alternatives. In this context, we will explore various approaches
5 min read
Mastering Search and Replace in Vi Editor
Vi Editor, a powerful text editor renowned for its efficiency and versatility, is a staple tool for Unix/Linux users. Mastering its search and replace functionalities can significantly enhance productivity and streamline text editing tasks. In this comprehensive guide, we will delve into various tec
6 min read
How to Replace Values in a List in Python?
Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using
2 min read
knn Impute Using Categorical Variables with caret Package
In data science and machine learning, missing data is a common issue that can significantly impact the performance of predictive models. One effective way to handle missing values is through imputation, which involves replacing missing data with substituted values. The caret package in R provides se
3 min read
Using REPLACE Function in SQL
In Structured Query Language (SQL), the REPLACE function is used to replace a substring or a part of a string within the given String. While dealing with some data pre-processing tasks or some data cleaning tasks, the REPLACE function is found to be very useful. It can save a lot of time and it also
4 min read
Replace Values Based on Condition in R
In this article, we will examine various methods to replace values based on conditions in the R Programming Language. How to replace values based on conditionR language offers a method to replace values based on conditions efficiently. By using these methods provided by R, it is possible to replace
3 min read
Replace Last Comma in Character with &-Sign in R
A string in R is a sequence of characters which contains numbers, characters, and special symbols. The characters can be modified, inserted as well as deleted in the strings. R provides a large variety of functions and external packages to carry out the string amendments. In this article, we are goi
4 min read