
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Rows with NA in R Data Frames Stored in a List
To remove rows that have NA in R data frames stored in a list, we can use lapply function along with na.omit function.
For example, if we have a list called LIST that contains some data frames each containing few missing values then the removal of rows having missing values from these data frames can be done by using the command given below −
lapply(LIST,na.omit)
Check out the below given example to understand how it works.
Example
Following snippet creates a sample data frame −
df1<-data.frame(x1=rnorm(20),x2=sample(c(NA,2,5),20,replace=TRUE),x3=rnorm(20)) df2<-data.frame(y1=rpois(20,2),x2=rpois(20,5),x3=sample(c(NA,3,1),20,replace=TRUE)) df3<-data.frame(z1=sample(c(NA,35,40),20,replace=TRUE),z2=rpois(20,1),z3=rpois(20,3)) List<-list(df1,df2,df3) List
Output
The following dataframes are created −
[[1]] x1 x2 x3 1 0.6335449 2 -0.22124641 2 0.3349365 5 -0.54942283 3 0.9501903 NA 0.16557880 4 1.5303796 2 -2.08566142 5 0.4384521 NA -0.67675747 6 -2.7142501 2 -0.12101118 7 -0.7043597 5 -0.56138587 8 0.5291248 NA -1.41423100 9 -1.4908927 2 -1.02494189 10 0.7306274 NA -0.27786609 11 0.2796165 2 1.09441626 12 0.1193108 2 -0.44195048 13 0.2438589 NA 0.79697076 14 0.7290668 NA -0.89008018 15 -1.2803967 5 1.24610229 16 -0.4271407 NA 0.12830629 17 -0.2131141 5 0.06398567 18 1.0359205 2 0.65339733 19 0.1116563 2 -2.34618128 20 -0.3049548 5 -0.45742539 [[2]] y1 x2 x3 1 0 8 3 2 2 7 3 3 2 6 3 4 0 9 NA 5 2 5 3 6 2 8 NA 7 1 2 3 8 0 6 1 9 1 8 NA 10 0 5 3 11 3 5 NA 12 3 6 NA 13 3 5 3 14 0 7 1 15 2 10 1 16 1 7 1 17 3 0 1 18 0 4 1 19 1 4 3 20 0 4 NA [[3]] z1 z2 z3 1 40 2 3 2 NA 0 1 3 35 1 2 4 40 2 3 5 35 2 1 6 NA 0 3 7 35 1 6 8 NA 1 2 9 35 2 4 10 35 2 0 11 40 2 2 12 35 1 2 13 NA 2 3 14 NA 2 5 15 NA 1 2 16 NA 1 2 17 40 1 2 18 35 1 3 19 40 0 0 20 40 2 5
Add the following code to the above snippet −
df1<-data.frame(x1=rnorm(20),x2=sample(c(NA,2,5),20,replace=TRUE),x3=rnorm(20)) df2<-data.frame(y1=rpois(20,2),x2=rpois(20,5),x3=sample(c(NA,3,1),20,replace=TRUE)) df3<-data.frame(z1=sample(c(NA,35,40),20,replace=TRUE),z2=rpois(20,1),z3=rpois(20,3)) List<-list(df1,df2,df3) lapply(List,na.omit)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[[1]] x1 x2 x3 1 0.6335449 2 -0.22124641 2 0.3349365 5 -0.54942283 4 1.5303796 2 -2.08566142 6 -2.7142501 2 -0.12101118 7 -0.7043597 5 -0.56138587 9 -1.4908927 2 -1.02494189 11 0.2796165 2 1.09441626 12 0.1193108 2 -0.44195048 15 -1.2803967 5 1.24610229 17 -0.2131141 5 0.06398567 18 1.0359205 2 0.65339733 19 0.1116563 2 -2.34618128 20 -0.3049548 5 -0.45742539 [[2]] y1 x2 x3 1 0 8 3 2 2 7 3 3 2 6 3 5 2 5 3 7 1 2 3 8 0 6 1 10 0 5 3 13 3 5 3 14 0 7 1 15 2 10 1 16 1 7 1 17 3 0 1 18 0 4 1 19 1 4 3 [[3]] z1 z2 z3 1 40 2 3 3 35 1 2 4 40 2 3 5 35 2 1 7 35 1 6 9 35 2 4 10 35 2 0 11 40 2 2 12 35 1 2 17 40 1 2 18 35 1 3 19 40 0 0 20 40 2 5
Advertisements