NA in subsetter - Inconsistent behavior
Asked Answered
C

1

1

Consider the following:

seq(from=10,to=30)[c(4,8)]
[1] 13 17

seq(from=10,to=30)[c(NA,8)]
[1] NA 17

seq(from=10,to=30)[c(NA,NA)]
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

To me the result of the latter is inconsistent with how the other two examples are handled by R and has cost me quite unpleasant debugging pains.

Might this be considered a bug?

Claudication answered 1/5, 2014 at 10:43 Comment(4)
A bug, no. None of the seq(10, 30) is NA. [ is an extraction function. The NA you are trying to extract are not present in the sequence.Pesach
Try seq(from=10,to=30)[c(TRUE, FALSE)] and see what you get.Pesach
Or try (10:30)[NA] to see what you get. R's rules about dealing with NA are quite explicit and are in fact consistent.Glib
I don't get that. (10:30)[c(NA,8)] might be paraphrased into: "Return the 'not applicable' and 8th element", so why is "return the 'not applicable' element twice" not returning c(NA,NA)?Claudication
B
2

Not sure if it is a bug - I suspect that depends on your point of view, but it is a subtlety of how indexing works. The quick solution is to change your third example to:

seq(from=10,to=30)[as.numeric(c(NA,NA))]

The reason is that c(NA,NA) is a logical vector, so the logical subsetting (which involves recycling the vector) is used, whereas having at least one non-NA causes the vector to be promoted to an integer vector. Likewise this could be implemented as:

seq(from=10,to=30)[c(NA_integer_,NA_integer_)]

See ?'[' for specifics of indexing if you're not familiar with them.

Blackdamp answered 1/5, 2014 at 12:10 Comment(1)
That makes it very clear - I had not appreciated the type difference the subsetting vector is subject to and always thought about NA as a kind of neutral type. Thank you very much.Claudication

© 2022 - 2024 — McMap. All rights reserved.