[R] surprising dates

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 4 03:34:48 CEST 2006


On 6/3/06, ivo welch <ivowel at gmail.com> wrote:
> I wonder if this is an intentional feature or an oversight.  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
>
>
> > 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
>

Note that ifelse says:

     A vector of the same length and attributes (including class) as
     'test'



More information about the R-help mailing list