The problem with your code is that NA are not character, but NA.
> a <- c("Kat", "cat", "Kat", NA)
> length(which(a == "Kat"))
[1] 2
>lengthwhichis.na(a)))
[1] 1
> length(which(a != "Kat"))
[1] 1
> length(which(!is.na(a)))
[1] 3
Above, is how you should do it, however
> length(which(a != "NA"))
[1] 3
> length(which(a == "NA"))
[1] 0
which might look strange. The first seem to work, the second doesn't. But they are both "wrong".
Basically this happens because in the first case there are three elements that are not character "NA". NA is not a character, so it does not count
In the second case, there are no elements that match with character "NA". Once again, NA is not a character, so it does not count.
Further reads:
http://cran.r-project.org/doc/manuals/R-intro.html#Missing-values
(the whole "book" is worth having in the bookmarks when working with R)
hi Jeremy, what if it was "Kat" rather than NA. is there an opposite for =="kat" (ie count every other element but kat)? Thanks
NA
are not character, butNA
, that is, I think, why your code does not work.