[R] documented (was surprising) dates

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Jun 4 08:28:30 CEST 2006


On Sat, 3 Jun 2006, ivo welch wrote:

> I wonder if this is an intentional feature or an oversight.

These are documented properties of the functions you are using.

> in some column summaries or in ifelse operations, apparently I am losing 
> the date property of my vector.
>
>> a <- c(198012, 198101, 198102)
>> b <- a*100+31
>> c <- as.Date( as.character(b), "%Y%m%d" )
>> summary(c)
>        Min.      1st Qu.       Median         Mean      3rd Qu.         Max.
> "1980-12-31" "1981-01-07" "1981-01-15" "1981-01-15" "1981-01-23" "1981-01-31"
>> summary( cbind(1:3, c) )
>       V1            c
> Min.   :1.0   Min.   :4017
> 1st Qu.:1.5   1st Qu.:4025
> Median :2.0   Median :4032
> Mean   :2.0   Mean   :4032
> 3rd Qu.:2.5   3rd Qu.:4040
> Max.   :3.0   Max.   :4048
>               NA's   :   1

You are creating a matrix from an integer and a date column.  Matrices can
only have one mode, here numeric.  If you meant to create a data frame, 
use data.frame, not cbind.

>> d <- a*100+28
>> e <- as.Date( as.character(d), "%Y%m%d" )
>> e
> [1] "1980-12-28" "1981-01-28" "1981-02-28"
>> c
> [1] "1980-12-31" "1981-01-31" NA
>> ifelse( is.na(c), e, c )
> [1] 4017 4048 4076   # date property is lost

As documented. From ?ifelse:

Value:

      A vector of the same length and attributes (including class) as
      'test' and data values from the values of 'yes' or 'no'.  The mode
      of the answer will be coerced from logical to accommodate first
      any values taken from 'yes' and then any values taken from 'no'.

Note that the class is taken from 'test'.

> PS: this time I do not need help.  I can write my code around this.

Help in pointing you to the posting guide and its recommended reading of 
the help page might still be helpful.

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