- 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 R data frame rows and keep the rows with NA in the output?
To subset R data frame rows and keep the rows with NA in the output, we can use subset function along with OR condition with | sign for na values.
For example, if we have a data frame called df that contains a column say C which has some NA values then we can subset df for values greater than 5 and include NA in the output by using the below given command −
subset(df,C>5|is.na(C))
Example 1
Following snippet creates a sample data frame −
x1<-sample(c(NA,1,2,3),20,replace=TRUE) y1<-sample(c(NA,1,2,3),20,replace=TRUE) df1<-data.frame(x1,y1) df1
The following dataframe is created −
x1 y1 1 3 3 2 2 2 3 1 1 4 2 2 5 1 NA 6 2 NA 7 1 3 8 2 3 9 2 2 10 1 3 11 NA 2 12 3 1 13 3 3 14 3 1 15 1 2 16 1 1 17 2 2 18 2 1 19 1 3 20 1 2
In order to subset df1 if x1 values less than 2 Without NA, add the following code to the above snippet −
x1<-sample(c(NA,1,2,3),20,replace=TRUE) y1<-sample(c(NA,1,2,3),20,replace=TRUE) df1<-data.frame(x1,y1) subset(df1,x1<2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 y1 3 1 1 5 1 NA 7 1 3 10 1 3 15 1 2 16 1 1 19 1 3 20 1 2
Moreover, to subset df1 if x1 values less than 2 With NA, add the following code to the above snippet −
x1<-sample(c(NA,1,2,3),20,replace=TRUE) y1<-sample(c(NA,1,2,3),20,replace=TRUE) df1<-data.frame(x1,y1) subset(df1,x1<2|is.na(x1))
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 y1 3 1 1 5 1 NA 7 1 3 10 1 3 11 NA 2 15 1 2 16 1 1 19 1 3 20 1 2
Example 2
Following snippet creates a sample data frame −
x2<-sample(c(NA,rpois(2,3)),20,replace=TRUE) y2<-sample(c(NA,rpois(2,5)),20,replace=TRUE) df2<-data.frame(x2,y2) df2
The following dataframe is created −
x2 y2 1 5 NA 2 1 3 3 1 6 4 5 3 5 1 6 6 5 3 7 1 6 8 5 6 9 5 6 10 5 NA 11 NA 6 12 1 3 13 5 NA 14 1 3 15 NA 6 16 1 6 17 5 3 18 NA NA 19 NA 3 20 5 6
In order to subset df2 if y2 values less than 6 Without NA, add the following code to the above snippet −
x2<-sample(c(NA,rpois(2,3)),20,replace=TRUE) y2<-sample(c(NA,rpois(2,5)),20,replace=TRUE) df2<-data.frame(x2,y2) subset(df2,y2<6)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x2 y2 2 1 3 4 5 3 6 5 3 12 1 3 14 1 3 17 5 3 19 NA 3
Moreover, to subset df2 if y2 values less than 6 With NA, add the following code to the above snippet −
x2<-sample(c(NA,rpois(2,3)),20,replace=TRUE) y2<-sample(c(NA,rpois(2,5)),20,replace=TRUE) df2<-data.frame(x2,y2) subset(df2,y2<6|is.na(y2))
Output
If you execute all the above given snippets as a single program, it generates the following output −
x2 y2 1 5 NA 2 1 3 4 5 3 6 5 3 10 5 NA 12 1 3 13 5 NA 14 1 3 17 5 3 18 NA NA 19 NA 3
- Related Articles
- How to subset rows of data frame without NA using dplyr in R?
- How to subset rows that do not contain NA and blank in one of the columns in an R data frame?
- How to subset rows of an R data frame using grepl function?
- How to select rows of an R data frame that are non-NA?
- How to subset rows that contains maximum depending on another column in R data frame?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to find the unique rows in an R data frame?
- How to subset R data frame rows if at least one or more values matches?
- How to delete rows in an R data frame?
- How to add rows in an R data frame?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to find the percent of NA’s in R data frame rows?
- How to convert rows in an R data frame to list?
- How to subset an R data frame by specifying columns that contains NA?
