- 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 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
- Related Articles
- How to select columns in R without missing values?
- How to find the row mean for columns in an R data frame by ignoring missing values?
- How to combine columns by excluding missing values in R?
- How to add two columns if both contains missing values in R?
- How to fill the missing values of an R data frame from the mean of columns?
- How to convert a column with missing values to binary with 0 for missing values in R?
- How to find the percentage of missing values in an R data frame?
- How to find the correlation matrix for a data frame that contains missing values in R?
- How to find the number of groupwise missing values in an R data frame?
- How to find the number of unique values in a vector by excluding missing values in R?
- Find the frequency of unique values and missing values for each column in an R data frame.
- How to find the starting position for consecutive values given the length of consecutive values in an R vector?
- How to find the sum of every n values if missing values exists in the R data frame?
- How to find the sum of non-missing values in an R data frame column?
- How to find the percentage of missing values in each column of an R data frame?

Advertisements