- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace missing values recorded with blank spaces in R with NA or any other value?
Sometimes when we read data in R, the missing values are recorded as blank spaces and it is difficult to replace them with any value. The reason behind this is we need to know how many spaces we have used in place of missing values. If we know that then assigning any value becomes easy.
Example
Consider the below data frame of vectors x and y.
> x<-c("", 3,2,1,2,3,2,1," ", 43, "") > y<-c(1,2,"", 43,2," ", 3,2,3,"", 7) > df<-data.frame(x,y) > df x y 1 1 2 3 2 3 2 4 1 43 5 2 2 6 3 7 2 3 8 1 2 9 3 10 43 11 7
Here, we have missing values recorded as blank spaces as well simply with double inverted commas. Now let’s replace these missing values with NA as shown below −
> df[df==""]<-NA > df x y 1 <NA> 1 2 3 2 3 2 <NA> 4 1 43 5 2 2 6 3 7 2 3 8 1 2 9 3 10 43 <NA> 11 <NA> 7
Here, the nineth value in x and sixth value in y are not replaced because the number of blank spaces, so we need to specify them. First, read the number of spaces by looking at the vectors as follows −
> x [1] "" "3" "2" "1" "2" "3" "2" "1" " " "43" "" > y [1] "1" "2" "" "43" "2" " " "3" "2" [9] "3" "" "7"
There seems to be one blank space for nineth value in x and five blank spaces in sixth value of y. Now let’s change the df for x as follows −
> df[df==" "]<-NA > df x y 1 <NA> 1 2 3 2 3 2 <NA> 4 1 43 5 2 2 6 3 7 2 3 8 1 2 9 <NA> 3 10 43 <NA> 11 <NA> 7
Now we will the df for y as shown below −
> df[df==" "]<-NA > df x y 1 <NA> 1 2 3 2 3 2 <NA> 4 1 43 5 2 2 6 3 <NA> 7 2 3 8 1 2 9 <NA> 3 10 43 <NA> 11 <NA> 7
Now, we have our complete data frame with NA’s and other numbers.
- Related Articles
- How to replace NA with 0 and other values to 1 in an R data frame column?
- How to replace NA values with zeros in an R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to fill missing values after merging the data frames with another value than NA in R?
- How to replace missing values with row means in an R data frame?
- How to replace missing values with linear interpolation method in an R vector?
- How to replace missing values with median in an R data frame column?
- How to replace 0 with NA in an R matrix?
- How to convert a column with missing values to binary with 0 for missing values in R?
- How to combine two vectors while replacing the NA values with the values in the other vector in R?
- How to replace leading zero with spaces - JavaScript
- How to fill a data.table row with missing values in R?
- How to fill NA values with previous values in an R data frame column?
- Set Blank spaces in column names with MySQL?
- How to deal with missing values to calculate correlation matrix in R?
