33

Say I have the following R data.frame ZZZ:

( ZZZ <- structure(list(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8, 
8)), .Names = c("n", "m", "o"), row.names = c(NA, -3L), class = "data.frame") )

## not run
   n  m o
1  1  6 7
2  2 NA 8
3 NA NA 8

I want to know, in the form of a vector, how many non-NAs I've got. I want the answer available to me as:

2, 1, 3

When I use the command length(ZZZ), I get 3, which of course is the number of vectors in the data.frame, a valuable enough piece of information.

I have other functions that operate on this data.frame and give me answers in the form of vectors, but, dang-it, length doesn't operate like that.

Blue Magister
  • 13,044
  • 5
  • 38
  • 56
Plsvn
  • 465
  • 2
  • 6
  • 9

4 Answers4

100
colSums(!is.na(x))

Vectorisation ftw.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
hadley
  • 102,019
  • 32
  • 183
  • 245
41

Try this:

# define "demo" dataset
ZZZ <- data.frame(n=c(1,2,NA),m=c(6,NA,NA),o=c(7,8,8))
# apply the counting function per columns
apply(ZZZ, 2, function(x) length(which(!is.na(x))))

Having run:

> apply(ZZZ, 2, function(x) length(which(!is.na(x))))
n m o 
2 1 3 

If you really insist on returning a vector, you might use as.vector, e.g. by defining this function:

nonNAs <- function(x) {
    as.vector(apply(x, 2, function(x) length(which(!is.na(x)))))
    }

You could simply run nonNAs(ZZZ):

> nonNAs(ZZZ)
[1] 2 1 3
daroczig
  • 28,004
  • 7
  • 90
  • 124
6

For getting total no of missing values use sum(is.na(x)) and for colum-wise use colSums(is.na(x)) where x is varible that contain dataset

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
3

If you only want the sum total of NAs overall, then sum() with !is.na() will do it:

ZZZ <- data.frame(n = c(1, 2, NA), m = c(6, NA, NA), o = c(7, 8, 8))
sum(!is.na(ZZZ))
kmm
  • 6,045
  • 7
  • 43
  • 53
  • I think the OP is after the number of NAs **per** column in the data frame not in overall. – daroczig Feb 13 '11 at 19:06
  • Thanks for everyone's help on this. It answers my question. I am only just beginning to open the door to see what R can do. It is truly an amazing tool. – Plsvn Feb 14 '11 at 04:13