

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to find the percent of NA’s in R data frame rows?
To find the percent of NAs in each row of an R data frame, we can follow the below steps −
First of all, create a data frame.
Then, use rowSums function and ncol function along with apply function to find the percent of NAs in each row of the data frame
Example
Create the data frame
Let’s create a data frame as shown below −
v1<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v2<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v3<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v4<-sample(c(NA,rpois(3,2)),25,replace=TRUE) df<-data.frame(v1,v2,v3,v4) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
v1 v2 v3 v4 1 1 2 2 4 2 2 2 2 3 3 2 2 NA 3 4 2 1 4 NA 5 2 2 NA 1 6 NA NA 4 NA 7 2 2 4 NA 8 2 1 4 3 9 2 2 0 4 10 2 2 0 NA 11 NA 2 2 NA 12 2 2 0 4 13 1 2 4 1 14 1 NA 4 3 15 2 NA 4 NA 16 2 NA 4 3 17 2 NA NA 3 18 1 1 NA 4 19 NA 1 NA NA 20 NA 2 0 4 21 1 2 4 1 22 2 2 2 1 23 2 1 4 4 24 1 2 4 NA 25 NA NA NA 3
Find the percent of NAs in each row
Using rowSums function and ncol function along with apply function to find the percent of NAs in each row of the data frame df −
v1<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v2<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v3<-sample(c(NA,rpois(3,2)),25,replace=TRUE) v4<-sample(c(NA,rpois(3,2)),25,replace=TRUE) df<-data.frame(v1,v2,v3,v4) df$NA_Percent<-rowSums(apply(is.na(df),2,as.numeric))/ncol(df) df
Output
v1 v2 v3 v4 NA_Percent 1 1 2 2 4 0.00 2 2 2 2 3 0.00 3 2 2 NA 3 0.25 4 2 1 4 NA 0.25 5 2 2 NA 1 0.25 6 NA NA 4 NA 0.75 7 2 2 4 NA 0.25 8 2 1 4 3 0.00 9 2 2 0 4 0.00 10 2 2 0 NA 0.25 11 NA 2 2 NA 0.50 12 2 2 0 4 0.00 13 1 2 4 1 0.00 14 1 NA 4 3 0.25 15 2 NA 4 NA 0.50 16 2 NA 4 3 0.25 17 2 NA NA 3 0.50 18 1 1 NA 4 0.25 19 NA 1 NA NA 0.75 20 NA 2 0 4 0.25 21 1 2 4 1 0.00 22 2 2 2 1 0.00 23 2 1 4 4 0.00 24 1 2 4 NA 0.25 25 NA NA NA 3 0.75
- Related Questions & Answers
- How to find the percent of NA’s in data.table object rows in R?
- How to find the number of NA’s in each column of an R data frame?
- How to subset rows of data frame without NA using dplyr in R?
- How to subset R data frame rows and keep the rows with NA in the output?
- How to find the percent of zeros in each row of an R data frame?
- How to select rows of an R data frame that are non-NA?
- How to find the column mean by excluding NA’s and if all values are NA then output NA in R data frame?
- How to find the unique rows in an R data frame?
- How to find the row sums if NA exists in the R data frame?
- How to find the frequency of NA values per row in an R data frame?
- How to find the standard deviation if NA’s are present in a column of an R data frame?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to find the correlation matrix for rows of an R data frame?
- How to replace NA’s to a value of selected columns in an R data frame?
- How to find the frequency of particular value in rows of an R data frame?
Advertisements