Emkin
Emkin

Reputation: 11

How to track which rows in a dataframe have NA values in R

I'm working now with some really big dataframes and in order to shorten the calculations I need to exclude rows with NA values, but after the calculation and creating new dataframe I need to bring them back (to the new dataframe). Is there any possibility to track which rows I exclude?

for example:

   X1 X2 X3 X4
1  3  4  5  4
2  3  3  3  4
3  NA NA NA NA
4  4  3  3  2

So what I want is to get after "complete.cases" number 3, that refers to the row number with NA that was excluded. Is it possible without adding extra column with numbering?

Upvotes: 0

Views: 1008

Answers (2)

mtoto
mtoto

Reputation: 24198

To return the indices of rows containing at least one NA we could also use:

rownames(df[rowSums(is.na(df)) > 0,])
# [1] "3"

Upvotes: 1

Julius Vainora
Julius Vainora

Reputation: 48231

You may use na.omit, then attr(na.omit(df), "na.action") gives 3.

Also,

rownames(df)[!complete.cases(df)]
# [1] "3"

Upvotes: 3

Related Questions