On 11/3/19 11:51 AM, Bert Gunter wrote:
Rui is right -- lubridate functionality and robustness is better -- but
just for fun, here is a simple function, poorly named reformat(), that
splits up the date formats, cleans them up and standardizes them a bit, and
spits them back out with a sep character of your choice (your original
split and recombine suggestion). Lubridate probably does something similar
but more sophisticated, but maybe it's worthwhile to see how one can do it
using basic functionality. This only requires a few short lines of code.

If one wants to investigate existing efforts at automatic date _and_ time reformatting, then do not forget Dirk's anytime package:


https://cran.r-project.org/web/packages/anytime/index.html


--

David.


reformat <- function(z, sep = "-"){
    z <- gsub(" ","",z) ## remove blanks
    ## break up dates into 3 component pieces and convert to matrix
    z <- matrix(unlist(strsplit(z, "-|/")), nrow = 3)
    ## add "0" in front of single digit in dd and mm
    ## add "20" in front  of "yy"
    for(i in 1:2) z[i, ] <- gsub("\\<([[:digit:]])\\>","0\\1",z[i, ])
    z[3, ] <- sub("\\<([[:digit:]]{2})\\>","20\\1",z[3, ])
    ## combine back into single string separated by sep
    paste(z[1, ],z[2, ],z[3, ], sep = sep)
}

## Testit
z <- c(" 1 / 22 /2015"," 1 -5 -15","11/7/2016", "14-07-16")
reformat(z)
[1] "01-22-2015" "01-05-2015" "11-07-2016" "14-07-2016"

reformat(z,"/")
[1] "01/22/2015" "01/05/2015" "11/07/2016" "14/07/2016"

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 Sun, Nov 3, 2019 at 12:15 AM Rui Barradas <ruipbarra...@sapo.pt> wrote:

Hello,

I believe the simplest is to use package lubridate. Its functions try
several formats until either one is right or none fits the data.

x <- c('11/7/2016', '14-07-16')
lubridate::dmy(x)
#[1] "2016-07-11" "2016-07-14"


The order dmy must be the same for all vector elements, if not

y <- c('11/7/2016', '14-07-16', '2016/7/11')
lubridate::dmy(y)
#[1] "2016-07-11" "2016-07-14" NA
#Warning message:
# 1 failed to parse.


Hope this helps,

Rui Barradas

Às 02:25 de 03/11/19, reichm...@sbcglobal.net escreveu:
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@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.

______________________________________________
R-help@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]]

______________________________________________
R-help@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.

______________________________________________
R-help@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.

Reply via email to