- 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 extract column names that do not have even one missing value in an R data frame?
The extraction of column names that do not have missing values can be done with the help of colnames function along with the complete.cases function. The complete.cases function will extract the columns that do not have missing values then colnames will extract those column names only.
Example1
Consider the below data frame −
x1<−sample(c(NA,1,0),20,replace=TRUE) x2<−rpois(20,5) x3<−sample(c(NA,5,10),20,replace=TRUE) df1<−data.frame(x1,x2,x3) df1
Output
x1 x2 x3 1 1 1 5 2 NA 9 NA 3 0 5 5 4 NA 3 NA 5 0 2 10 6 NA 4 10 7 0 4 NA 8 NA 5 5 9 0 3 10 10 1 1 5 11 0 2 5 12 1 2 10 13 1 7 10 14 NA 8 5 15 1 1 NA 16 NA 3 NA 17 0 9 5 18 0 5 5 19 1 7 NA 20 1 4 10
Extracting column name that do not have missing values −
colnames(df1)[complete.cases(t(df1))]
Output
[1] "x2"
Example2
y1<−letters[1:20] y2<−sample(c(NA,"A","B","C"),20,replace=TRUE) y3<−sample(c(NA,rnorm(5)),20,replace=TRUE) df2<−data.frame(y1,y2,y3) df2
Output
y1 y2 y3 1 a A 1.2176106 2 b A NA 3 c A 0.7633109 4 d <NA> −0.3289873 5 e C 0.7633109 6 f C −0.3289873 7 g C −0.9952887 8 h <NA> 1.2176106 9 i B 1.2176106 10 j <NA> −0.9952887 11 k <NA> NA 12 l A 0.6808376 13 m B 0.7633109 14 n B −0.9952887 15 o C 0.7633109 16 p A NA 17 q C 1.2176106 18 r C −0.9952887 19 s A 0.7633109 20 t B 1.2176106
Extracting column name that do not have missing values −
Example
colnames(df2)[complete.cases(t(df2))]
Output
[1] "y1"
Advertisements