Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count NAs per row in dataframe [duplicate]

Tags:

dataframe

r

na

I've got dataframe that has batch ID and the results of six tests performed on each batch. The data looks like this:

batch_id  test1  test2  test3  test4  test5  test6
001       0.121     NA  0.340  0.877  0.417  0.662
002       0.229  0.108     NA  0.638     NA  0.574

(there are a few hundred rows in this dataframe, only one row per batch_id)

I'm looking for a way to count how many NAs there are for each batch_id (for each row). I feel like this should be do-able with a few lines of R code at the most, but I'm having trouble actually coding it. Any ideas?

like image 314
Shark7 Avatar asked Jun 14 '16 00:06

Shark7


People also ask

How do you count Na in a row?

1. Count the Number of NA's per Row with rowSums() The first method to find the number of NA's per row in R uses the power of the functions is.na() and rowSums(). Both the is.na() function and the rowSums() function are R base functions.

How do I count rows in R?

The nrow() function in R programming R provides us nrow() function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.

How do I get the row count in a Dataframe in R?

To get number of rows in R Data Frame, call the nrow() function and pass the data frame as argument to this function. nrow() is a function in R base package.


2 Answers

You can count the NAs in each row with this command:

rowSums(is.na(dat))

where dat is the name of your data frame.

like image 165
Sven Hohenstein Avatar answered Oct 07 '22 10:10

Sven Hohenstein


You could add a new column to your data frame containing the number of NA values per batch_id:

df$na_count <- apply(df, 1, function(x) sum(is.na(x)))
like image 33
Tim Biegeleisen Avatar answered Oct 07 '22 12:10

Tim Biegeleisen