Determine the number of NA values in a column
I want to count the number of NA
values in a data frame column. Say my data frame is called df
, and the name of the column I am considering is col
. The way I have come up with is following:
sapply(df$col, function(x) sum(length(which(is.na(x)))))
Is this a good/most efficient way to do this?
Solution 1:
You're over-thinking the problem:
sum(is.na(df$col))
Solution 2:
If you are looking for NA
counts for each column in a dataframe then:
na_count <-sapply(x, function(y) sum(length(which(is.na(y)))))
should give you a list with the counts for each column.
na_count <- data.frame(na_count)
Should output the data nicely in a dataframe like:
----------------------
| row.names | na_count
------------------------
| column_1 | count