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!
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!
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