[R] transforming dates

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sun Nov 3 03:56:25 CET 2019


Well, one way to do it is via regex's -- no splitting and recombining
needed.
Note: This will convert a factor into a character vector.

> z <- c("11/7/2016", "14-07-16")
> z <- gsub("-([[:digit:]]{2})-([[:digit:]]{2})", "/\\1/20\\2",z) ## /\ is
/ and \
> z
[1] "11/7/2016"  "14/07/2016"

I leave it to you as an exercise to either convert 7 to 07 or vice-versa if
you want to do this.
Note, if you have spaces sprinkled inconsistently around your separators,
you'll have to work a bit harder with your regex.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Nov 2, 2019 at 7:25 PM <reichmanj using sbcglobal.net> wrote:

> R-Help Forum
>
>
>
> I have a data set that contains a date field but the dates are in two
> formats
>
>
>
> 11/7/2016            dd/mm/yyyy
>
> 14-07-16               dd-mm-yy
>
>
>
> How would I go about correcting this problem. Should I separate the dates,
> format them , and then recombine?
>
>
>
> Sincerely
>
>
>
> Jeff Reichman
>
> (314) 457-1966
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list