Re: [Rd] Inconsistency when naming a vector

2015-04-28 Thread Hadley Wickham
On Mon, Apr 27, 2015 at 8:33 AM, peter dalgaard  wrote:
>
>> On 27 Apr 2015, at 13:48 , Hadley Wickham  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/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Inconsistency when naming a vector

2015-04-27 Thread peter dalgaard

> On 27 Apr 2015, at 13:48 , Hadley Wickham  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

> a <- "a"
> a[[5]] <- "b"
> a
[1] "a" NA  NA  NA  "b"

and even

> a <- NULL
> a[[5]] <- "a"
> a
[1] NA  NA  NA  NA  "a"

also, we have

> l <- list(1,2,3)
> names(l) <- c("a","b")
> l
$a
[1] 1

$b
[1] 2

$
[1] 3

and we do want to obey general rules like

names(l)[[2]] <- "a" 

being (nearly) equivalent to

`*tmp*`<- names(l)
`*tmp*`[[2]] <- "a"
names(l) <- `*tmp*`


- pd

> 
> Hadley
> 
> -- 
> http://had.co.nz/
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Inconsistency when naming a vector

2015-04-27 Thread Kevin Ushey
In `?names`:

 If ‘value’ is shorter than ‘x’, it is extended by character ‘NA’s
 to the length of ‘x’.

So it is as documented.

That said, it's somewhat surprising that both NA and "" serve as a
placeholder for a 'missing name'; I believe they're treated
identically by R under the hood (e.g. in subsetting operations) but
there may be some subtle cases where they're not.


On Mon, Apr 27, 2015 at 6:08 AM, Suzen, Mehmet  wrote:
>
> There is no inconsistency. Documentation of `names` says "...value
> should be a character vector of up to the same length as x..."
> In the first definition your character vector is not the same length
> as length of x, so you enforce NA by not defining value[2]
>
> x <- 1:2
> value<-c("a")
> value[2]
> [1] NA
>
> where as in the second case, R uses default value "", from `names`
> documentation "..The name "" is special: it is used to indicate that
> there is no name associated with an element.". Since you defined the
> first one, it internally assigns "" to non-defined names to match the
> length of the vector.
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Inconsistency when naming a vector

2015-04-27 Thread Suzen, Mehmet
There is no inconsistency. Documentation of `names` says "...value
should be a character vector of up to the same length as x..."
In the first definition your character vector is not the same length
as length of x, so you enforce NA by not defining value[2]

x <- 1:2
value<-c("a")
value[2]
[1] NA

where as in the second case, R uses default value "", from `names`
documentation "..The name "" is special: it is used to indicate that
there is no name associated with an element.". Since you defined the
first one, it internally assigns "" to non-defined names to match the
length of the vector.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Inconsistency when naming a vector

2015-04-27 Thread Hadley Wickham
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.

Hadley

-- 
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel