[6510] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: Undesirable behavior of c()

daemon@ATHENA.MIT.EDU (Charles C. Berry)
Fri Feb 24 13:17:19 1995

Date: Fri, 24 Feb 95 09:30:46 PST
From: cberry@tajo.UCSD.EDU (Charles C. Berry)
To: 74273.3130@compuserve.com
Cc: S-news@utstat.toronto.edu

Steven P. Millard said:

> 
> I have come across an undesirable behavior of the c() function:
> 
> > dum <- 1:3
> 
> > names(dum) <- letters[1:3]
> 
> > dum
>  a b c 
>  1 2 3
> 
> > c(v1 = dum["a"], v2 = dum["c"])
>  v1.a v2.c 
>     1    3
> 
> > c(v1 = dum[1], v2 = dum[3])
>  v1.a v2.c 
>     1    3
> 
> Now why did S-PLUS add the '.a' to 'v1' and the '.c' to 'v2'?  Yes, I understand that 
> when I subscript dum, the 'names' attribute will be retained, but in this case I 
> specifically told c() that the names of the elements should be 'v1' and 'v2'.  
> While S-PLUS may be trying to be helpful by reminding me where v1 and v2 came from, 
> this is a very unexpected behavior and I don't like it.  Please change it!
> 
> I realize the work-around is as follows:
> 
> > dum2 <- dum[c("a", "c")]
> > names(dum2) <- c("v1", "v2")
> > dum2
>  v1 v2 
>   1  3
> 
> --Steve M.

Although you may not have desired this behavior, it is not obvious how
one would undo it and still preserve the full functionality of c().

Using your example, suppose this vector is desired:

> c(v1=dum[1:2],v2=dum[2:3])
 v1.a v1.b v2.b v2.c 
    1    2    2    3
> 

Clearly, unique names are needed and "v1" and "v2" are not unique. 

Of course one could adopt a convention that whenever the arguments of
c() are atomic, of length 1, and named, that the names of the elements
be discarded. However, this implies an assumption that the original
names are no longer needed, which might not be the case.


I wonder if any existing functions would be "broken" by replacing the
existing version of c() with one that behaves as you suggest.

Another way of getting the behavior you desire is:

> c(v1=as.vector(dum[1]),v2=as.vector(dum[3]))
 v1 v2 
  1  3


Charles C. Berry
cberry@tajo.ucsd.edu

home help back first fref pref prev next nref lref last post