[R] convert 'character' vector containing mixed formats to 'Date'

Petr PIKAL petr.pikal at precheza.cz
Thu Jun 21 15:10:47 CEST 2012


Hi

> 
> Dear all
> I have a 'character' vector containing mixed formats (thanks Excel!)
> and I'd like to translate it into a default "%Y-%m-%d" Date vector.
> x <- c("1/3/2005", "13/04/2004", "2/5/2005", "2/5/2005", "7/5/2007",
>        "22/04/2004", "21/04/2005", "20080430", "13/05/2003", "20080529",
>        NA, NA, "19/05/1999", "17/05/2000", "17/05/2000")
> 
> 
> In the above you will see that some dates are of format="%d/%m/%Y",
> others of format="%Y%m%d" and some NA values. Can you suggest a
> straight-forward way of transforming these to a uniform 'character' or
> 'Date' vector? I tried to do the following, but it outputs very
> strange results:
> > x
>  [1] "1/3/2005"   "13/04/2004" "2/5/2005"   "2/5/2005"   "7/5/2007"
> "22/04/2004"
>  [7] "21/04/2005" "20080430"   "13/05/2003" "20080529"   NA
> NA
> [13] "19/05/1999" "17/05/2000" "17/05/2000"
> > sum(xa <- grepl('/', x))
> [1] 11
> > sum(xb  <- grepl('200', substr(x, 1,4)))
> [1] 2
> > sum(xc <- is.na(x))
> [1] 2
> > x[xa] <- as.Date(x[xa], format="%d/%m/%Y")
> > x[xb] <- as.Date(x[xb], format="%Y%m%d")
> > x
>  [1] "12843" "12521" "12905" "12905" "13640" "12530" "12894" "13999"
> "12185" "14028"
> [11] NA      NA      "10730" "11094" "11094"
> 

You can use another as.Date with origin specified.

as.Date(ifelse(ind, as.Date(x, format="%d/%m/%Y"), as.Date(x, 
format="%Y%m%d")) , origin="1970-01-01")
 [1] "2005-03-01" "2004-04-13" "2005-05-02" "2005-05-02" "2007-05-07"
 [6] "2004-04-22" "2005-04-21" "2008-04-30" "2003-05-13" "2008-05-29"
[11] NA           NA           "1999-05-19" "2000-05-17" "2000-05-17"
 
Regards
Petr

> 
> The culprit is likely that the 'x' vector is 'character' throughout,
> but I'm not sure how to work around. For example, I couldn't figure
> how to create an empty 'Date' vector. Regards
> Liviu
> 
> 
> -- 
> Do you know how to read?
> http://www.alienetworks.com/srtest.cfm
> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> Do you know how to write?
> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
> 
> ______________________________________________
> 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.



More information about the R-help mailing list