Determine the number of NA values in a column

user3274289 picture user3274289 · Jun 4, 2014 · Viewed 387.8k times · Source

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?

Answer

rrs picture rrs · Jun 4, 2014

You're over-thinking the problem:

sum(is.na(df$col))