- 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 set NA values to TRUE for a Boolean column in an R data frame?
Dealing with NA values is one of the boring and almost day to day task for an analyst and hence we need to replace it with the appropriate value. If in an R data frame, we have a Boolean column that represents TRUE and FALSE values, and we have only FALSE values then we might want to replace NA’s with TRUE. In this case, we can use single square bracket and is.na to set all NA’s to TRUE.
Example
set.seed(999) S.No.<-1:20 Won<-sample(c(FALSE,NA),20,replace=TRUE) df1<-data.frame(S.No.,Won) df1
Output
S.No. Won 1 1 FALSE 2 2 NA 3 3 FALSE 4 4 FALSE 5 5 FALSE 6 6 NA 7 7 FALSE 8 8 NA 9 9 NA 10 10 NA 11 11 FALSE 12 12 FALSE 13 13 NA 14 14 FALSE 15 15 FALSE 16 16 FALSE 17 17 NA 18 18 NA 19 19 FALSE 20 20 FALSE
Setting NA’s in the Won column to TRUE −
df1["Won"][is.na(df1["Won"])]<-TRUE df1
Output
S.No. Won 1 1 FALSE 2 2 TRUE 3 3 FALSE 4 4 FALSE 5 5 FALSE 6 6 TRUE 7 7 FALSE 8 8 TRUE 9 9 TRUE 10 10 TRUE 11 11 FALSE 12 12 FALSE 13 13 TRUE 14 14 FALSE 15 15 FALSE 16 16 FALSE 17 17 TRUE 18 18 TRUE 19 19 FALSE 20 20 FALSE
Let’s have a look at another example −
Example
Group<-sample(1:4,20,replace=TRUE) Hot<-sample(c(FALSE,NA),20,replace=TRUE) df2<-data.frame(Group,Hot) df2
Output
Group Hot 1 3 FALSE 2 1 NA 3 3 NA 4 2 FALSE 5 2 FALSE 6 4 NA 7 3 NA 8 2 NA 9 3 FALSE 10 3 NA 11 3 NA 12 4 FALSE 13 4 NA 14 3 FALSE 15 4 FALSE 16 1 NA 17 4 NA 18 1 FALSE 19 2 NA 20 3 NA
Setting NA’s in the Hot column to TRUE −
df2["Hot"][is.na(df2["Hot"])]<-TRUE df2
Output
Group Hot 1 2 TRUE 2 1 FALSE 3 1 FALSE 4 3 TRUE 5 2 FALSE 6 4 FALSE 7 1 FALSE 8 3 TRUE 9 1 TRUE 10 3 TRUE 11 1 TRUE 12 1 TRUE 13 2 TRUE 14 2 TRUE 15 4 FALSE 16 4 FALSE 17 1 TRUE 18 3 FALSE 19 3 FALSE 20 3 TRUE
- Related Articles
- How to fill NA values with previous values in an R data frame column?
- How to set a level of a factor column in an R data frame to NA?
- 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 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?
- Set values in categorical column to numeric values in R data frame.
- How to fill the NA values from above row values in an R data frame?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to select positive values in an R data frame column?
- How to randomly replace values in an R data frame column?
- How to convert a string in an R data frame to NA?
- How to calculate row means by excluding NA values in an R data frame?
- How to extract columns based on particular column values of an R data frame that match\na pattern?
- How to convert NaN to NA in an R data frame?

Advertisements