Luigi Marongiu
2020-Jun-10 08:20 UTC
[R] How to convert European short dates to ISO format?
Hello, 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), thus I get: ``` 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") isoDates = as.Date(oriDates, format = "%m/%d/%y")> isoDates[1] NA NA NA NA NA NA NA [8] NA NA "2020-01-02" "2020-02-02" "2020-03-02" "2020-04-02" "2020-05-02" [15] "2020-06-02" "2020-07-02" ``` How can I convert properly? -- Best regards, Luigi
On Wed, 10 Jun 2020 10:20:01 +0200 Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> the function as.Dates interprets them as American ones (mm/dd/yy)> isoDates = as.Date(oriDates, format = "%m/%d/%y")> How can I convert properly?Pass the correct format? (Swap m and d in the format string.) -- Best regards, Ivan
Daniel Nordlund
2020-Jun-10 08:29 UTC
[R] How to convert European short dates to ISO format?
On 6/10/2020 1:20 AM, Luigi Marongiu wrote:> isoDates = as.Date(oriDates, format = "%m/%d/%y")You need to use the format for European short dates. isoDates = as.Date(oriDates, format = "%d/%m/%y") Hope this is helpful, Dan -- Daniel Nordlund Port Townsend, WA USA
Hi! Should it be: as.Date(oriDates, format="%d/%m/%y") # See the order of %d and %m!! This command seems to work for me, here the output: [1] "2020-01-23" "2020-01-24" "2020-01-25" "2020-01-26" "2020-01-27" "2020-01-28" "2020-01-29" "2020-01-30" [9] "2020-01-31" "2020-02-01" "2020-02-02" "2020-02-03" "2020-02-04" "2020-02-05" "2020-02-06" "2020-02-07" HTH, Kimmo ke, 2020-06-10 kello 10:20 +0200, Luigi Marongiu kirjoitti:> Hello, > 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), thus I get: > > ``` > 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") > isoDates = as.Date(oriDates, format = "%m/%d/%y") > > isoDates > > [1] NA NA NA NA NA > NA NA > [8] NA NA "2020-01-02" "2020-02-02" "2020-03-02" > "2020-04-02" "2020-05-02" > [15] "2020-06-02" "2020-07-02" > ``` > > How can I convert properly?
Berend Hasselman
2020-Jun-10 08:30 UTC
[R] How to convert European short dates to ISO format?
Luigi, Try format = "%d/%m/%y" Berend Hasselman> On 10 Jun 2020, at 10:20, Luigi Marongiu <marongiu.luigi at gmail.com> wrote: > > ISO 8601
Heinz Tuechler
2020-Jun-10 08:34 UTC
[R] How to convert European short dates to ISO format?
maybe isoDates <- as.Date(oriDates, format = "%d/%m/%y") Heinz Luigi Marongiu wrote/hat geschrieben on/am 10.06.2020 10:20:> Hello, > 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), thus I get: > > ``` > 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") > isoDates = as.Date(oriDates, format = "%m/%d/%y") >> isoDates > [1] NA NA NA NA NA > NA NA > [8] NA NA "2020-01-02" "2020-02-02" "2020-03-02" > "2020-04-02" "2020-05-02" > [15] "2020-06-02" "2020-07-02" > ``` > > How can I convert properly? >
Luigi Marongiu
2020-Jun-10 08:36 UTC
[R] How to convert European short dates to ISO format?
Thank you! On Wed, Jun 10, 2020 at 10:29 AM Daniel Nordlund <djnordlund at gmail.com> wrote:> > On 6/10/2020 1:20 AM, Luigi Marongiu wrote: > > isoDates = as.Date(oriDates, format = "%m/%d/%y") > > You need to use the format for European short dates. > > isoDates = as.Date(oriDates, format = "%d/%m/%y") > > > Hope this is helpful, > > Dan > > -- > Daniel Nordlund > Port Townsend, WA USA >-- Best regards, Luigi
Jeff Newmiller
2020-Jun-10 14:37 UTC
[R] How to convert European short dates to ISO format?
Fix your format specification? ?strptime On June 10, 2020 1:20:01 AM PDT, Luigi Marongiu <marongiu.luigi at gmail.com> wrote:>Hello, >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), thus I get: > >``` >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") >isoDates = as.Date(oriDates, format = "%m/%d/%y") >> isoDates > [1] NA NA NA NA NA >NA NA > [8] NA NA "2020-01-02" "2020-02-02" "2020-03-02" >"2020-04-02" "2020-05-02" >[15] "2020-06-02" "2020-07-02" >``` > >How can I convert properly?-- Sent from my phone. Please excuse my brevity.
On Wed, 10 Jun 2020, Jeff Newmiller wrote:> Fix your format specification? > ?strptime>> 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), thus I get:Look at Hadley Wickham's 'tidyverse' collection as described in R for Data Science. There are date, datetime, and time functions that will do just what you want. Rich
Rasmus Liland
2020-Jun-11 14:48 UTC
[R] How to convert European short dates to ISO format?
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>
Luigi Marongiu
2020-Jun-12 06:45 UTC
[R] How to convert European short dates to ISO format?
Thank you. It worked. Case closed On Thu, 11 Jun 2020, 16:48 Rasmus Liland, <jral at posteo.no> wrote:> 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 >[[alternative HTML version deleted]]