0

I've done some digging, but can't seem to find anything that specifically touches on why the output returns: Code: c(0:5)[NA] Output: NA NA NA NA NA NA

Any help would be much appreciated!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • Please add the full code, data, and expected output. See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for how to make the question better. – NelsonGon Feb 25 '21 at 07:29

1 Answers1

0

NA is of type logical.

class(NA)
[1] "logical"

Logicals have a special property in R called as vector recycling. If you subset a vector with a smaller subset it basically recycles the same value.

Example -

c(0:5)[c(TRUE, FALSE)] #returns
#[1] 0 2 4

#similarly

c(0:5)[c(FALSE, TRUE)] #returns
#[1] 1 3 5

The same is happening here the logical NA value is recycled for the entire length (0:5 here) and you get NA of the same length.

c(0:5)[NA]
[1] NA NA NA NA NA NA

It would be give a different output for other NA's.

c(0:5)[NA_integer_]
#[1] NA

c(0:5)[NA_real_]
#[1] NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213