[R] Bug in "transform"?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Dec 2 20:42:13 CET 2008


Prof Brian Ripley wrote:
> As the help page says
> 
>      If some of the values are not vectors of the appropriate length,
>      you deserve whatever you get!

Yes (did I write that?). It is a bit annoying with things that almost 
work, though.


[snip]
> 
>> 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.
>

...at least when you're not making use of the scoping aspects. And if 
you calculate at least one vector of full length, then the issue goes away.



 > transform(aq, a=1,b=2)
Error in data.frame(`_data`, e[!matched]) :
   arguments imply differing number of rows: 6, 1
 > transform(aq, a=1,b=2,o=Ozone)
     Ozone Solar.R Wind Temp Month Day a b  o
3      12     149 12.6   74     5   3 1 2 12
31     37     279  7.4   76     5  31 1 2 37
34     NA     242 16.1   67     6   3 1 2 NA
65     NA     101 10.9   84     7   4 1 2 NA
59     NA      98 11.5   80     6  28 1 2 NA
133    24     259  9.7   73     9  10 1 2 24



The underlying issue is actually not in transform() but in data.frame():

 > aq <- airquality[sample(1:153,6),]
 > data.frame(aq, list(a=1,b=2))
Error in data.frame(aq, list(a = 1, b = 2)) :
   arguments imply differing number of rows: 6, 1
 > data.frame(aq, list(a=1))
     Ozone Solar.R Wind Temp Month Day a
3      12     149 12.6   74     5   3 1
31     37     279  7.4   76     5  31 1
34     NA     242 16.1   67     6   3 1
65     NA     101 10.9   84     7   4 1
59     NA      98 11.5   80     6  28 1
133    24     259  9.7   73     9  10 1



-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list