[R] How to preserve date format while aggregating

Erich Studerus erich.studerus at bli.uzh.ch
Mon Sep 8 15:55:52 CEST 2008


Here's how the dates look like after the sql-query:

Oav$Date[1:3]
[1] "1991-11-22 00:45:00 CET" "1991-12-13 00:01:00 CET" "1992-02-06 00:45:00
CET"

> class(oav$Date[1:3])
[1] "POSIXt"  "POSIXct"

> x<-as.numeric(oav$Date[1:3])
> x
[1] 690767100 692578860 697333500

> class(x)<-"Date"
> x
[1] "3226-01-31" "8186-07-07" "1204-04-11"

I wanteded to apply the aggregate function in R instead of in the SQL-query,
because R is much more flexible and faster in manipulating large dataframes.

Erich

 

-----Ursprüngliche Nachricht-----
Von: Dr Eberhard Lisse [mailto:el at lisse.NA] 
Gesendet: Montag, 8. September 2008 15:43
An: Erich Studerus
Cc: r-help at r-project.org
Betreff: Re: [R] How to preserve date format while aggregating

Erich,

how does the data look, when it comes from SQL?

And why not extract the data with SQL directly, so you don't
have this issue in the first place?

el

on 9/8/08 3:15 PM Erich Studerus said the following:
> Thanks, I've already tried that. The problem is, that the original date is
> not restored when I change the numeric back to date. I get a totally
> different date.
> Maybe it has something to do with the original date format. My data are
> directly imported from a SQL-database. The date column to which I want to
> apply the aggregate function has the two classes POSIXt and POSIXct.
> Changing the column to class "Date" before applying the aggregate function
> did not help. I still get a different date, when I transform it back to
> class "Date".
> I would be glad, if someone knew a more elegant way to extract the rows
with
> minimum/earliest date per subject. 
> 
> 
> Erich
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: jim holtman [mailto:jholtman at gmail.com] 
> Gesendet: Montag, 8. September 2008 14:24
> An: Erich Studerus
> Cc: r-help at r-project.org
> Betreff: Re: [R] How to preserve date format while aggregating
> 
> Try changing the 'class' of the numeric result back to Date:
> 
>> x <- as.Date('2008-09-08')
>> x
> [1] "2008-09-08"
>> y <- as.numeric(x)
>> y
> [1] 14130
>> str(y)
>  num 14130
>> class(y) <- "Date"
>> y
> [1] "2008-09-08"
>> str(y)
> Class 'Date'  num 14130
> 
> 
> On Mon, Sep 8, 2008 at 6:38 AM, Erich Studerus
> <erich.studerus at bli.uzh.ch> wrote:
>> Hi
>>
>> I have a dataframe in which some subjects appear in more than one row. I
>> want to extract the subject-rows which have the minimum date per subject.
> I
>> tried the following aggregate function.
>>
>> attach(dataframe.xy)
>>
>> aggregate(Date,list(SubjectID),min)
>>
>> Unfortunately, the format of the Date-column changes to numeric, when I'm
>> applying this function. How can I preserve the date format?
>>
>> Thanks
>>
>> Erich
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 
> 

-- 
Dr. Eberhard W. Lisse  \        / Obstetrician & Gynaecologist (Saar)
el at lisse.NA el108-ARIN / *     |   Telephone: +264 81 124 6733 (cell)
PO Box 8421             \     /   Please do NOT email to this address
Bachbrecht, Namibia     ;____/        if it is DNS related in ANY way



More information about the R-help mailing list