R Subsetting Specific Value Also Returns NA?


I am just starting out on learning R and came across a piece of code as follows

vec_1 <- c("a","b", NA, "c","d") # create a subet of all elements which equal "a" vec_1[vec_1 == "a"]

The result from this is

## [1] "a" NA

Im just curious, since I am subsetting vec_1 for the value "a", why does NA also show up in my results?


This is because the result of anything == NA is NA. Even NA == NA is NA.

Here's the output of vec_1 == "a" -


and NA is not TRUE or FALSE so when you subset anything by NA you get NA. Check this out -

vec_1[NA] [1] NA NA NA NA NA

When dealing with NA, R tries to provide the most informative answer i.e. T | NA returns TRUE because it doesn't matter what NA is. Here are some more examples -

T | NA [1] TRUE F | NA [1] NA T & NA [1] NA F & NA [1] FALSE

R has no way to test equality with NA. In your case you can use %in% operator -

5 %in% NA [1] FALSE "a" %in% NA [1] FALSE vec_1[vec_1 %in% "a"] [1] "a"


  • How to subset a large data frame (ffdf) in R by date?
  • Return only columns containing NA in R
  • Data read separately fscanf
  • How to subset a data.frame if the column contains NAs [duplicate]
  • Drop variable in panel data in R conditional based on a defined number of consecutive observations
  • How do I revert sys.stdout.close()?
  • How to override List.Add method?
  • unexpected behaviour of Google BigQuery WHERE NOT list CONTAINS string
  • Primefaces lazy datascroller calling load twice
  • Where can I find tomesh.c?
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • What is the correct way to synchronize a shared, static object in Java?
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • How can I display the parent menu item's description using Wordpress walkers?
  • How to disable all widgets inside Panel or inside Composite?
  • MySQL Order by column = x, column asc?
  • Is playing sound in Javascript performance heavy?
  • Insert into database using onclick function
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • Knitr HTML Loop - Some HTML output, some R output
  • Deserializing XML into class C#
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • php design question - will a Helper help here?
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?