![]() |
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:
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’.
Output: a b c In the below example, we created a matrix using the function ‘matrix()’. After we are replaced the NAs with a interger 5.
Output: [,1] [,2] [,3] 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 .
Output: [1] "Before changing the vector" 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.
Output: [,1] [,2] [,3] 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’.
Output: [1] "The dataframe is" In the below example, we created a vector. Using the function ‘replace()’, we replaced the NAs with a interger 8.
Output: [1] "Before changing the vector" ConclusionIn 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. |
Reffered: https://www.geeksforgeeks.org
R Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |