Skip to content
Prev 62158 / 63424 Next

Should subsetting named vector return named vector including named unmatched elements?

Never been a big fan of this behavior either but maybe the intention was 
to make it easier to distinguish between 2 types of NAs in the result: 
those that were present in the original vector vs those that are 
introduced by an unmatched subscript. Like in this example:

 ??? x <- setNames(c(101:108, NA), letters[1:9])
 ??? x
 ??? # ? a?? b?? c?? d?? e?? f?? g?? h?? i
 ??? # 101 102 103 104 105 106 107 108? NA

 ??? x[c("g", "k", "a", "i")]
 ??? #? ? g <NA>??? a??? i
 ??? #? 107?? NA? 101?? NA

The first NA is the result of an unmatched subscript, while the second 
one comes from 'x'.

This is of limited interest though. In most real world applications I've 
worked on, we actually need to "fix" the names of the result.

Best,

H.
On 1/18/24 11:51, Ji?? Moravec wrote: