You're not subsetting using equality, you are coercing the numerics 1:10
to logical--and any numeric other than 0 is coerced to TRUE
. Run, e.g.,
!(1:10)
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
You get 10 FALSE
s, so when you subset a any vector of length 10 with 10 FALSE
s, you get nothing.
As documented in ?TRUE
and ?NA
, a logical comparison with NA
results in NA
.
And, of course, 0 is coerced to FALSE
, so !0
is coerced to TRUE, so when you set the 6th element to 0,
!c(1:5, 0, 7:10)
# [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
# 1 2 3 4 5 ^^^6 7 8 9 10
You get a TRUE
in the 6th position, so subsetting with that will return the 6th element.
How is 0 and NA both NOT y?
You might be looking for y[y != y]
?