[R] HMisc describe -- error with dates

Gabor Grothendieck
Wed, 19 Nov 2003 01:35:44 -0000

Since Frank Harrell indicates that he will likely have addressed
the chron issue in Hmisc by next week you may just want to
wait for that; however, if you need an interim solution for
representing chron dates in such a manner that they sort and
display correctly under the Hmisc describe function then here
are two workarounds.

Assume z is your vector of chron dates.


1. Character variables

Represent them as character variables using a format, such as
yyyy/mm/dd , that sorts correctly under character comparisons:

  chr <- substring( paste(years(z),chron(z),sep="/"), 1, 10 )
  describe(chr)

This will convert chr back to chron:

 chron( paste(substring(chr,6),substring(chr,3,4),sep="/") )


2. Factors

Represent them as factors like this:

  zs <- chron(sort(unique(z)))
  f <- factor(z,lev=zs,lab=zs)
  describe(f)

This will convert f back to chron:

  chron(as.character(f))


---

From: Frank E Harrell Jr <feh3k at spamcop.net>
To: <tmurph6 at po-box.mcgill.ca>
Cc: <r-help at stat.math.ethz.ch>
Subject: Re: [R] HMisc describe -- error with dates



On Tue, 18 Nov 2003 13:47:17 -0500
tmurph6 at po-box.mcgill.ca wrote:

> Thank you Gabor and Frank for you replies last week. Unfortunately I
> still don't get it (I'm kind of new at this). Just to recap:
I'm
> starting with a Julian date and want results presented as 11NOV2003, for
> example (or some other date format with no time component).
>
> The HMisc describe function accepts variables that I have now formatted
> like this
>
> >date.ddmmmyy(aidsdate)
>
> whereas it does not like variables formatted like this
>
> >dates(aidsdate,format="day.mon.year", origin=c(month = 1, day
= 1, year
> >> 1960))
>
> which is what I was using with an earlier version of Hmisc.
>
> I wasn't able to figure out how to use posix.
>
> The problem now is that, unlike before, the dates are not sorted (at
> least not chronologically--maybe like character variables) and I liked
> seeing the 5 lowest and highest dates in the describe output.
>
> Sorry for pestering, but it is a testimony to how much I like Hmisc :)
>
> Thanks!
>
> Tanya
>
>
>

Tanya,

I want to fix this in general. It's on my todo list for next week.
-Frank


> Quoting Frank E Harrell Jr <feh3k at spamcop.net>:
>
> > On Tue, 11 Nov 2003 20:40:50 -0500 (EST)
> > "Gabor Grothendieck" <ggrothendieck at myway.com>
wrote:
> >
> > >
> > >
> > > dates is part of chron. There may be something wrong in
> > > Hmisc's detection of chron objects.
> > >
> > > > chron(1)
> > > [1] 01/02/70
> > >
> > > > isChron(chron(1))
> > > FALSE
> > >
> > > ---
> > >
> > > Date: Tue, 11 Nov 2003 20:06:38 -0500
> > > From: <tmurph6 at po-box.mcgill.ca>
> > > To: Frank E Harrell Jr <feh3k at spamcop.net>
> > > Cc: <r-help at stat.math.ethz.ch>
> > > Subject: Re: [R] HMisc describe -- error with dates
> > >
> > >
> > >
> > > I am using the chron package. I have no preference for which
> > > function I use. I just want the most reliable format for HMisc
> > > functions and general plotting. I like the ddmonyyy formats (e.g.
> > > 11NOV2003). What would you recommend?
> >
> > I like POSIX but chron should be workable. The fix for isChron in
> > Hmisc is:
> >
> > function(x) {
> > cl <- class(x)
> > dc <- if(.R.)
c('POSIXt','POSIXct','dates','times','chron')
else
> >
c('timeDate','date','dates','times','chron')
> > length(cl) && any(cl %in% dc)
> > }
> >
> > But describe also runs this command:
> >
> > if(isdatetime) notime <-
all(format(x.unique,"%H%M%S")=='000000')
> >
> > and such calls to format for chron objects (which actually calls
> > format.dates if x.unique is a dates object) do not work. So a few
> > more modifications will need to be made in describe to work with chron
> > objects, when time allows.
> >
> > Frank
> >
> > >
> > > Thank you!
> > >
> > > Quoting Frank E Harrell Jr <feh3k at spamcop.net>:
> > >
> > > > On Tue, 11 Nov 2003 18:28:24 -0500
> > > > tmurph6 at po-box.mcgill.ca wrote:
> > > >
> > > > > Hello,
> > > > >
> > > > > I am trying to use HMisc describe on a data frame. I
have
> > > > > specified certain variables as dates using, for
example:
> > > > >
> > > > > df1$aidsdate <-
dates(aidsdate,format="day.mon.year",
> > > > > origin=c(month = 1, day = 1, year = 1960))
> > > > >
> > > > > When I use describe on the dataframe I get this error:
> > > > >
> > > > > Error in Ops.dates(weights, x) : * not defined for
chron objects
> > > > >
> > > > > Has anyone had this problem? I had used the same method
with an
> > > > > older version of R and HMisc and it worked.
> > > > >
> > > > > Should be be formatting date variables differently?
> > > > >
> > > > > Thank you!
> > > > >
> > > > > Sincerely,
> > > > > Tanya Murphy
> > > > >
> > > >
> > > > In recent versions, describe for R recognizes date/times by
one of
> > > > the following classes:
'POSIXt','POSIXct','chron'. If you really
> > > > need to use another date format I could be talked into
extending
> > > > the code for that if you can remind me where dates( ) is
found.
> > > > -Frank
> > > >
> > > > ---
> > > > Frank E Harrell Jr Professor and Chair School of Medicine
> > > > Department of Biostatistics Vanderbilt University
> > > >
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> >
> > ---
> > Frank E Harrell Jr Professor and Chair School of
> > Medicine
> > Department of Biostatistics Vanderbilt
> > University