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

Liviu Andronic landronimirc at gmail.com
Thu Jun 21 15:08:13 CEST 2012


On Thu, Jun 21, 2012 at 2:48 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
> 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
>

I think I managed to crack this by myself. I only needed to add an
as.character() call:
> x[xa] <- as.character(as.Date(x[xa], format="%d/%m/%Y"))
> x[xb] <- as.character(as.Date(x[xb], format="%Y%m%d"))
> x
 [1] "2005-03-01" "2004-04-13" "2005-05-02" "2005-05-02" "2007-05-07"
"2004-04-22"
 [7] "2005-04-21" "2008-04-30" "2003-05-13" "2008-05-29" NA
NA
[13] "1999-05-19" "2000-05-17" "2000-05-17"


Liviu



More information about the R-help mailing list