[R] How to convert European short dates to ISO format?

Rasmus Liland jr@| @end|ng |rom po@teo@no
Thu Jun 11 16:48:51 CEST 2020


On 2020-06-10 10:20 +0200, Luigi Marongiu wrote:
> I have been trying to convert European 
> short dates formatted as dd/mm/yy into the 
> ISO 8601 but the function as.Dates 
> interprets them as American ones 
> (mm/dd/yy)

Dear Luigi,

?strptime says:

	‘%D’ Date format such as ‘%m/%d/%y’: the C99 standard says it
	     should be that exact format (but not all OSes comply).

as.Date(oriDates, format="%d/%m/%y") works 
fine for me here on the Linux laptop ... 

You could also try to work on the strings 
themselves to convert them to ISO format, 
like:

	oriDates = c("23/01/20", "24/01/20", "25/01/20", "26/01/20",
	             "27/01/20", "28/01/20", "29/01/20", "30/01/20",
	             "31/01/20", "01/02/20", "02/02/20", "03/02/20",
	             "04/02/20", "05/02/20", "06/02/20", "07/02/20")
	d <- t(as.data.frame(strsplit(oriDates, "/")))
	dimnames(d) <- list(NULL, c("d", "m", "y"))
	u <- unique(d[,"y"])
	millenia <- "20"
	if (length(u)==1 & u==millenia) {
	  Dates <- paste0(millenia, d[,"y"], "-",
	                  d[,"m"], "-", d[,"d"])
	  Dates <- as.Date(Dates)
	} else {
	  Dates <- "'Allo 'allo ... error, error ... more complicated formatting is required to digest those dates ..."
	}
	print(Dates)

Best,
Rasmus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200611/1108b6bb/attachment.sig>


More information about the R-help mailing list