- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 convert a string in an R data frame to NA?
We often see mistakes in data collection processes and these mistakes might lead to incorrect results of the research. When the data is collected with mistakes, it makes the job of analyst difficult. One of the situations, that shows the data has mistakes is getting strings in place of numerical values. Therefore, we need to convert these strings to NA in R so that we can proceed with our intended analysis.
Example
Consider the below data frame −
> x1<-rep(c(1,3,6,7,5,2,"XYZ",12,4,5),times=2) > x2<-rep(c(67,"XYZ",45,32,52),each=4) > df<-data.frame(x1,x2) > df x1 x2 1 1 67 2 3 67 3 6 67 4 7 67 5 5 XYZ 6 2 XYZ 7 XYZ XYZ 8 12 XYZ 9 4 45 10 5 45 11 1 45 12 3 45 13 6 32 14 7 32 15 5 32 16 2 32 17 XYZ 52 18 12 52 19 4 52 20 5 52
Converting all XYZ’s to NA −
> df[df=="XYZ"]<-NA > df x1 x2 1 1 67 2 3 67 3 6 67 4 7 67 5 5 <NA> 6 2 <NA> 7 <NA> <NA> 8 12 <NA> 9 4 45 10 5 45 11 1 45 12 3 45 13 6 32 14 7 32 15 5 32 16 2 32 17 <NA> 52 18 12 52 19 4 52 20 5 52
Let’s have a look at one more example −
> ID<-c("Class",2:20) > ID<-c("Class",1:19) > Group<-rep(c("Class",2,3,4,5),times=4) > df1<-data.frame(ID,Group) > df1 ID Group 1 Class Class 2 1 2 3 2 3 4 3 4 5 4 5 6 5 Class 7 6 2 8 7 3 9 8 4 10 9 5 11 10 Class 12 11 2 13 12 3 14 13 4 15 14 5 16 15 Class 17 16 2 18 17 3 19 18 4 20 19 5 > df1[df1=="Class"]<-NA > df1 ID Group 1 <NA> <NA> 2 1 2 3 2 3 4 3 4 5 4 5 6 5 <NA> 7 6 2 8 7 3 9 8 4 10 9 5 11 10 <NA> 12 11 2 13 12 3 14 13 4 15 14 5 16 15 <NA> 17 16 2 18 17 3 19 18 4 20 19 5
- Related Articles
- How to convert NaN to NA in an R data frame?
- How to convert empty values to NA in an R data frame?
- How to convert NaN values to NA in an R data frame?
- How to convert an old data frame to new data frame in R?
- How to replace NA values with zeros in an R data frame?
- How to check which value is NA in an R data frame?
- How to convert a character data frame to numeric data frame in R?
- How to set a level of a factor column in an R data frame to NA?
- How to convert rows in an R data frame to list?
- How to set NA values to TRUE for a Boolean column in an R data frame?
- How to convert a data frame to data.table in R?
- How to select rows of an R data frame that are non-NA?
- How to subset an R data frame by specifying columns that contains NA?
- How to calculate row means by excluding NA values in an R data frame?
- How to fill NA values with previous values in an R data frame column?

Advertisements