[Rd] Naming difference in cbind between S-Plus (PR#6515)

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jan 31 23:49:09 MET 2004


On Sat, 31 Jan 2004, Duncan Murdoch wrote:

> On Sat, 31 Jan 2004 22:27:21 +0100 (CET), you wrote:
> 
> >Naming difference in cbind between S-Plus and R.
> >I think R is wrong.
> 
> I'm not sure if R is right or wrong, but I suspect the difference
> isn't in cbind, it's elsewhere...

It is in cbind and its methods.

> >abc <- data.frame(y=1:4, x=rnorm(4))
> >abc.lm <- lm(y ~ x, data=abc)
> >predict.lm(abc.lm, type="terms")  ## this is where R got the name "x"
> >abc <- cbind(abc, d=abc$y - predict.lm(abc.lm, type="terms"))
> 
> ... in the line above.  In S-PLUS, what does
> 
> abc$y - predict.lm(abc.lm, type="terms")
> 
> give?  

It gives an object of class "structure", a matrix with an attribute.

> In R it gives a matrix with the column name "x".  It makes
> sense to me that using cbind on a matrix with named columns should
> keep those column names.  
> 
> What does S-PLUS do in this case:
> 
> M <- matrix(1:4, 2, 2, dimnames=list(NULL, c('a', 'b')))
> cbind(c=5:6,d=M)
> 
> R gives 
> 
> > M <- matrix(1:4, 2, 2, dimnames=list(NULL, c('a', 'b')))
> > cbind(c=5:6,d=M)
>      c a b
> [1,] 5 1 3
> [2,] 6 2 4
> 
> which seems reasonable to me, though I might like the name "d" to be
> incorporated somehow.

The same as R.  However, the help page for cbind in R explicitly says
matrices and data frames are treated differently.

The difference is that S-PLUS gives

> cbind(abc, d=abc$y - predict.lm(abc.lm, type="terms"))
  y          x        d
1 1  0.3045526 1.143936
2 2 -1.1890024 1.865356
3 3 -1.0242794 2.896081
4 4  0.0401956 4.094628
> cbind(as.matrix(abc), d=abc$y - predict.lm(abc.lm, type="terms"))
  y          x        x
1 1  0.3045526 1.143936
2 2 -1.1890024 1.865356
3 3 -1.0242794 2.896081
4 4  0.0401956 4.094628

without any mention of a difference.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list