[R] Using transform to add a date column to a dataframe

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Dec 23 16:45:16 CET 2008


On Tue, 23 Dec 2008, Tom La Bone wrote:

>
>
> Gavin Simpson wrote:
>>
>>
>> It says that the two arguments have different numbers of observations.
>> The reason for which should now be pretty obvious as you provided a
>> single Date whereas airquality has 153 observations.
>>
>
>
> Thanks. I did look at ?transform but I was a bit confused because this
> worked
>
>   data1 <- transform(airquality,LTMDA=T)

What is "T"?  (If you mean 'TRUE', please say so, as "T" is a regular 
variable.)

> whereas this did not
>
>  data1 <- transform(airquality,Date=as.Date("1950-01-01"))
>
> Why does the first one work with one argument but the second one does not?

A Date object is not a vector.  In released versions of R, data.frame() 
only knows how to replicate vectors and factors.

As Gavin pointed out, you were warned so why are you disregarding the 
warning?  That a subclass works does not mean that you are entitled to 
leap to conclusions about another subclass, does it?  Abductive inference 
does not apply (it would be like useRs considering you to be always in 
error based on your email address).

-- 
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