[Rd] Inconsistency when naming a vector
Hadley Wickham
h.wickham at gmail.com
Tue Apr 28 18:24:42 CEST 2015
On Mon, Apr 27, 2015 at 8:33 AM, peter dalgaard <pdalgd at gmail.com> wrote:
>
>> On 27 Apr 2015, at 13:48 , Hadley Wickham <h.wickham at gmail.com> wrote:
>>
>> Sometimes the absence of a name is maked by an NA:
>>
>> x <- 1:2
>> names(x)[[1]] <- "a"
>> names(x)
>> # [1] "a" NA
>>
>> Whereas other times its
>>
>> y <- c(a = 1, 2)
>> names(y)
>> # [1] "a" ""
>>
>> Is this deliberate? The help for names() is a bit murky, but an
>> example shows the NA behaviour.
>
> I think it is
>
> (a) impossible to change
> (b) at least somewhat coherent
>
> The situation is partially due to the fact that character-NA is a relative latecomer to the language. In the beginning, there was no real distinction between NA and "NA", causing issues when abbreviating Noradrenaline, North America, Nelson Anderson, etc. At some point, it was decided to fix things up, as far as possible in a backawards compatible way. Some common idioms were retained but others were changed to comply with the rules for other vector types.
>
> We have the empty string convention on (AFAICT) all constructor usages:
>
> c(a=1, 3)
> list(a=1, 3)
> cbind(a=1, 3)
>
> and also in the lists implied by argument matching
>
>> f <- function(...) names(match.call(expand.dots=TRUE))
>> f(a=1,3)
> [1] "" "a" ""
>
> In contrast, assignment forms have the NA convention. This is consistent with the general rules for complex assignment. E.g. we have
>
Ah, that explanation makes sense. Thanks.
It would be helpful to have a isNamed function that abstracted over
all these differences:
isNamed <- function(x) {
nms <- names(x)
if (is.null(nms)) return(rep(FALSE, length(x))
!is.na(x) && x != ""
}
Hadley
--
http://had.co.nz/
More information about the R-devel
mailing list