- 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 subset rows that do not contain NA and blank in one of the columns in an R data frame?
It is possible that we get data sets where a column contains NA as well as blank, therefore, it becomes necessary to deal with these values. One of the ways to deal with these values is selecting the rows where we do not have them. This can be done by subsetting through single square brackets.
Example
Consider the below data frame −
> set.seed(1) > x1<-sample(1:50,20) > x2<-rep(c(1,"",3,4),times=5) > x3<-rep(c(5,NA,10,"",20),each=4) > df<-data.frame(x1,x2,x3) > df x1 x2 x3 1 4 1 5 2 39 5 3 1 3 5 4 34 4 5 5 23 1 <NA> 6 43 <NA> 7 14 3 <NA> 8 18 4 <NA> 9 33 1 10 10 21 10 11 41 3 10 12 10 4 10 13 7 1 14 9 15 15 3 16 40 4 17 25 1 20 18 47 20 19 12 3 20 20 36 4 20
Subsetting rows where x3 is neither NA nor blank −
> df[!(is.na(df$x3) | df$x3==""), ] x1 x2 x3 1 4 1 5 2 39 5 3 1 3 5 4 34 4 5 9 33 1 10 10 21 10 11 41 3 10 12 10 4 10 17 25 1 20 18 47 20 19 12 3 20 20 36 4 20
Subsetting rows where x2 is not blank −
> df[!(df$x2==""), ] x1 x2 x3 1 4 1 5 3 1 3 5 4 34 4 5 5 23 1 <NA> 7 14 3 <NA> 8 18 4 <NA> 9 33 1 10 11 41 3 10 12 10 4 10 13 7 1 15 15 3 16 40 4 17 25 1 20 19 12 3 20 20 36 4 20
- Related Articles
- How to subset an R data frame by specifying columns that contains NA?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to subset R data frame rows and keep the rows with NA in the output?
- How to subset rows of data frame without NA using dplyr in R?
- How to subset factor columns in an R data frame?
- How to select rows of an R data frame that are non-NA?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to find rows in an R data frame that do not have missing values?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to subset columns that has less than four categories 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 subset rows of an R data frame using grepl function?
- How to convert columns of an R data frame into rows?
- How to select rows of a data frame that are not in other data frame in R?
- How to select columns of an R data frame that are not in a vector?

Advertisements