I have a factor that I'm using as a lookup table.
condLookup = c(hotdog = "ketchup", ham = "mustard", popcorn = "salt", coffee = "cream")
This works as expected - I put in a 3-vector and get a 3-vector back:
condLookup[c("hotdog", "spinach", NA)]
hotdog <NA> <NA>
"ketchup" NA NA
This too is expected, even tho the returns are all NA
:
condLookup[c(NA, "spinach")]
<NA> <NA>
NA NA
And this:
condLookup["spinach"]
<NA>
NA
But then this surprised me - I gave an atomic NA, or two NA, and I got a named vector of 4 NA's back.
condLookup[NA]
<NA> <NA> <NA> <NA>
NA NA NA NA
condLookup[c(NA, NA)]
<NA> <NA> <NA> <NA>
NA NA NA NA
Apparently, for vector2 <- condLookup[vector1]
then vector2
will be the same length as vector1
unless every element in vector1
is NA. In which case vector2
is the same length as condLookup
.
Can you explain this behavior?
NA
is used to indexinteger
vector, with similar recycling: Indexing integer vector with NA – Euxenite