 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
How to find the length of columns for missing values in R?
The length of columns for missing values means the number of missing values in the data frame. This can be easily done with the help of colSums function where we will find the total number of NA values with is.na. For example, if we have a data frame called df that contains some missing values then the length of columns for missing values can be found by using the command colSums(is.na(df)).
Example1
Consider the below data frame −
> x1<-sample(c(1,NA),20,replace=TRUE) > x2<-sample(c(5,NA),20,replace=TRUE) > x3<-sample(c(2,NA),20,replace=TRUE) > x4<-sample(c(2,NA),20,replace=TRUE) > df1<-data.frame(x1,x2,x3,x4) > df1
Output
x1 x2 x3 x4 1 NA NA 2 2 2 NA NA NA 2 3 1 NA 2 NA 4 NA 5 NA NA 5 1 5 NA NA 6 NA 5 NA 2 7 1 NA NA 2 8 1 5 NA NA 9 NA NA 2 NA 10 1 5 NA NA 11 NA NA NA NA 12 NA NA 2 2 13 1 NA NA 2 14 1 NA NA 2 15 NA NA NA NA 16 1 NA NA NA 17 1 5 NA NA 18 NA NA 2 NA 19 1 NA NA NA 20 1 NA 2 2
Finding the length of columns for missing values in df1 −
> colSums(is.na(df1))
Output
x1 x2 x3 x4 9 14 14 12
Example2
> y1<-sample(c(101,NA),20,replace=TRUE) > y2<-sample(c(325,NA),20,replace=TRUE) > y3<-sample(c(250,NA),20,replace=TRUE) > df2<-data.frame(y1,y2,y3) > df2
Output
y1 y2 y3 1 101 325 NA 2 NA NA NA 3 101 NA NA 4 101 325 250 5 NA NA NA 6 101 325 250 7 101 325 NA 8 NA 325 250 9 101 325 250 10 NA 325 NA 11 101 325 250 12 NA NA 250 13 101 NA NA 14 NA 325 NA 15 NA 325 NA 16 NA NA NA 17 NA NA NA 18 101 325 250 19 101 NA NA 20 NA 325 250
Finding the length of columns for missing values in df2 −
> colSums(is.na(df2))
Output
y1 y2 y3 10 8 12
Advertisements
 