[R] Bug in "transform"?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Dec 2 17:37:44 CET 2008


As the help page says

      If some of the values are not vectors of the appropriate length,
      you deserve whatever you get!

So you can use

mtcars1 <- mtcars
mtcars1[c("t1", "t2")] <- cbind(rep(3,32), rep(4, 32))

or even

mtcars1 <- transform(mtcars, t1=rep(3, 32), t2=rep(4, 32))

Vitalie Spinu wrote:
> Dear useRs,
> 
> Here is a weird behavior of transform function:
> 
>   mtcars1<-matcars
>   transform(mtcars1,t1=3,t2=4)
> Error in data.frame(`_data`, e[!matched]) :
>   arguments imply differing number of rows: 32, 1
> 
> instead, this works:
> 
>   mtcars1$t1<-0
>   transform(mtcars1,t1=3,t2=4)
> 
> also works if applied in turn:
> 
>   transform(mtcars1,t1=3)
>   transform(mtcars1,t2=4)

'works'?  Only if you assign the result.

> I often need to use this for creating new variables in data frame from 
> those already present.
> Sorely needed!!

Just learn to use indexing: transform() is just syntactic sugar that you 
are not making use of.

> Best,
> Vitalie.


-- 
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-help mailing list