[R] Coercing a dataframe column to datetime

richard.kittler@amd.com richard.kittler at amd.com
Wed Jun 16 17:13:34 CEST 2004


I understand I can do it directly with only one column.  It was meant to be a simple case to illustrate my problem.  I need to do it over many columns in the real application.

--Rich

Richard Kittler 
AMD TDG
408-749-4099

-----Original Message-----
From: Petr Pikal [mailto:petr.pikal at precheza.cz] 
Sent: Tuesday, June 15, 2004 11:10 PM
To: Kittler, Richard
Cc: r-help at stat.math.ethz.ch
Subject: RE: [R] Coercing a dataframe column to datetime




On 15 Jun 2004 at 11:31, richard.kittler at amd.com wrote:

> Thank you! The next step in the conversion still fails and I can't 
> seem to find any examples in the archives.  The result of the function 
> 'as.POSIXct(strptime())' within the 'sapply' comes back as numeric 
> rather than POSIXct as expected:
> 
> > ds <- cbind(1:2, c("02/27/92 23:03:20", "02/27/92 22:29:56")); ds
>      [,1] [,2]               
> [1,] "1"  "02/27/92 23:03:20"
> [2,] "2"  "02/27/92 22:29:56"
> > q <- sapply(ds[,2], function(x) as.POSIXct(strptime(x,"%m/%d/%y
> > %H:%M:%S"))) class(q)
> [1] "numeric"
> > q
> 02/27/92 23:03:20 02/27/92 22:29:56 
>         699260600         699258596 

Hi

Why do you use sapply? ds is included in some list? If not you can 
apply as.POSIXct directly to ds.

as.POSIXct(strptime(ds[,2],"%m/%d/%y %H:%M:%S"))

If you use sapply (or apply) the result is a vector or array which 
has to have the same class for all its elements. Therefore you get 
numeric representation of our dates (I suppose :-).

Cheers
Petr

> 
> --Rich
> 
> Richard Kittler
> AMD TDG
> 408-749-4099
> 
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: Monday, June 14, 2004 12:05 PM
> To: Kittler, Richard
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Coercing a dataframe column to datetime
> 
> 
> You have forgotten as.POSIXct is needed too.
> 
> On Mon, 14 Jun 2004 richard.kittler at amd.com wrote:
> 
> > I am trying to coerce a data frame column from character to datetime 
> > using strptime but keep getting an error because the length of the
> > coerced object is always 9.  What am I doing wrong here:   
> > 
> > .................................................................
> > > ds <- cbind(1:2, c("02/27/92 23:03:20", "02/27/92 22:29:56")); ds
> >      [,1] [,2]               
> > [1,] "1"  "02/27/92 23:03:20"
> > [2,] "2"  "02/27/92 22:29:56"
> > >  
> > > q <- strptime(ds[,2], "%m/%d/%y %H:%M:%S"); q
> > [1] "1992-02-27 23:03:20" "1992-02-27 22:29:56"
> > > 
> > > ds[,2] <- q
> > Error in "[<-"(`*tmp*`, , 2, value = q) : number of items to replace 
> > is not a multiple of replacement length
> > > 
> > > length(q)
> > [1] 9
> > 
> > .................................................................
> > 
> > --Rich
> > 
> > Richard Kittler
> > AMD TDG
> > 408-749-4099
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> > 
> > 
> 
> -- 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list