- 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 of data frame without NA using dplyr in R?
To subset rows of data frame without NA using dplyr in R, we can follow the below steps −
- First of all, create a data frame.
- Then, use filter function of dplyr package to subset the rows with !is.na.
Create the data frame
Let's create a data frame as shown below −
x<-sample(c(NA,1,2),20,replace=TRUE) df<-data.frame(x) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x 1 2 2 2 3 1 4 1 5 1 6 NA 7 1 8 NA 9 NA 10 1 11 1 12 1 13 2 14 1 15 NA 16 2 17 2 18 2 19 NA 20 1
Subset the rows of data frame without NA using dplyr
Using filter function of dplyr package to subset the rows of df with !is.na as shown below −
x<-sample(c(NA,1,2),20,replace=TRUE) df<-data.frame(x) library(dplyr) df %>% filter(!is.na(x))
Output
x 1 2 2 2 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 11 1 12 2 13 2 14 2 15 1
- Related Articles
- How to subset R data frame rows and keep the rows with NA in the output?
- How to collapse data frame rows in R by summing using dplyr?
- How to subset a data frame by excluding a column using dplyr in R?
- How to subset rows of an R data frame using grepl function?
- How to remove multiple rows from an R data frame using dplyr package?
- How to create a subset of a data frame in R without using column names?
- How to subset rows that do not contain NA and blank in one of the columns in an R data frame?
- How to subset an R data frame by specifying columns that contains NA?
- How to extract columns of a data frame in R using dplyr package?
- How to select rows of an R data frame that are non-NA?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to select rows with group wise minimum or maximum values of a variable in an R data frame using dplyr?
- How to find the mean of row values in an R data frame using dplyr?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?

Advertisements