[R] vector of factors to POSIXlt

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Feb 6 23:54:08 CET 2004


"Siddique, Amer" <Amer.Siddique at ssa.gov> writes:

> hello,
> I have a vector of factors
> > str(rcptdt)
>  Factor w/ 51 levels "1/10/03","1/13/03",..:
> > length(rcptdt)
> [1] 87
> 
> which i want to convert to class POSIXlt to extract the day, so:
> a1<-format(rcptdt,"%m/%d/%y")

Eh? Why? 

A simple as.character() should do it. The format string has no effect.

> > length(a1)
> [1] 87
> 
> and:
> a2<-strptime(a1, "%m/%d/%y")
> str(a2)
> `POSIXlt', format: chr [1:87] "2002-04-18" "2002-07-19" "2002-08-02"
> "2002-08-14" ...
> > a2[1]-a2[2]
> Time difference of -92 days
> 
> but the length differs 
> > length(a2)
> [1] 9
> 
> and i cant update rcptdt...

as.POSIXct(...) is needed, I believe.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list