Skip to content
Prev 371967 / 398500 Next

Problem Subsetting Rows that Have NA's

Some of us do share (or at least remember feeling) your pain. The ?Extract page is long and complex and there are several features that I find non-intuitive. But they are deemed desirable by others. I think I needed to read that page about ten times (with multiple different problems that needed explication) before it started to sink in. You are apparently on that same side of the split opinions on the feature of returning rows with logical NA's as I am. I've learned to use `which`, and I push back when the conoscienti says it's not needed.

 After you read it a few more times you may come to a different opinion. Many people come to R with preconceived notions of what words like "equals" or "list" or "vector" mean and then complain about the documentation. You would be better advised to spend more time studying the language. The help pages are precise but terse, and you need to spend time with the examples and with other tutorial material to recognize the gotcha's.

Here's a couple of possibly helpful rules regarding "[[" and "[" and logical indexing:

Nothing _equals_ NA.
Selection operations with NA logical index item return NA.  (Justified as a warning feature as I understand it.)
"[" always returns a list.
"[[" returns only one thing, but even that thing could be a list.
Generally you want "[[" if you plan on testing for equality with a vector.

The "R Inferno" by Burns is an effort to detail many more of the unexpected or irregular aspects of R (mostly inherited from S).