[R] Date Calculation

Martin Maechler maechler at stat.math.ethz.ch
Thu Jun 17 10:27:02 CEST 2004


>>>>> "KKWa" == Ko-Kang Kevin Wang <k.wang at auckland.ac.nz>
>>>>>     on Thu, 17 Jun 2004 19:57:39 +1200 writes:

    KKWa> Hi,
    KKWa> I've been playing with:
    >> joinDate <- format(strptime(as.vector(forum[,2]), "%d-%b-%y"),
    KKWa> +                    "%d-%b-%Y")
    >> today <- format(strptime(as.vector("14-Jun-04"), "%d-%b-%Y"),
    KKWa> +                 "%d-%b-%Y")
    >> joinDate
    KKWa> [1] "04-Feb-2004" "13-Feb-2004" "26-Feb-2004" "27-Feb-2004"
    KKWa> "27-Feb-2004"
    KKWa> [6] "27-Feb-2004" "29-Feb-2004" "01-Mar-2004" "02-Mar-2004"
    KKWa> "07-Mar-2004"
    KKWa> ................................
    >> today
    KKWa> [1] "14-Jun-0004"
    >> joinDate - today
    KKWa> Error in joinDate - today : non-numeric argument to binary operator

    KKWa> But it didn't quite work.  What I'd like joinDate -
    KKWa> today to return is the number of days to today, since
    KKWa> joinDate.  I'm sure it has been asked before however a
    KKWa> search on r-help didn't found me any relevant
    KKWa> information *_*.

Kevin, 
       [[did you have tough day? usually your Q/A are much better ;-()]]

both joinDate and today are results of format() and hence
character vectors (with no time information left).  
You didn't really expect  "-" to work with characters?

OTOH,  "-" properly does work with POSIX*t objects,
see, e.g.,  ?difftime

Regards,
Martin




More information about the R-help mailing list