Importing excel file with read_excel function: Date columns is not correctly imported









up vote
1
down vote

favorite












I did the following code to import a excel file in Rstudio:



(nms <- names(read_excel("myexcelfile.xlsx")))
(ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]


The resul is this dataframe below:



structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))


The first column should be the dates (Brazilian Format: Day/Month/Year).



enter image description here



How can I fix this?










share|improve this question

























    up vote
    1
    down vote

    favorite












    I did the following code to import a excel file in Rstudio:



    (nms <- names(read_excel("myexcelfile.xlsx")))
    (ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
    read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]


    The resul is this dataframe below:



    structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
    ), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
    -5L), class = c("tbl_df", "tbl", "data.frame"))


    The first column should be the dates (Brazilian Format: Day/Month/Year).



    enter image description here



    How can I fix this?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I did the following code to import a excel file in Rstudio:



      (nms <- names(read_excel("myexcelfile.xlsx")))
      (ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
      read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]


      The resul is this dataframe below:



      structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
      ), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
      -5L), class = c("tbl_df", "tbl", "data.frame"))


      The first column should be the dates (Brazilian Format: Day/Month/Year).



      enter image description here



      How can I fix this?










      share|improve this question













      I did the following code to import a excel file in Rstudio:



      (nms <- names(read_excel("myexcelfile.xlsx")))
      (ct <- ifelse(grepl("^Date", nms), "text", "numeric"))
      read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]


      The resul is this dataframe below:



      structure(list(Data = c("41731", "41730", "41729", "41726", "41725"
      ), ABEV3 = c(15.2, 14.9, 15.22, 15.15, 15.18)), row.names = c(NA,
      -5L), class = c("tbl_df", "tbl", "data.frame"))


      The first column should be the dates (Brazilian Format: Day/Month/Year).



      enter image description here



      How can I fix this?







      r tidyverse






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 22:41









      Diogo Bastos

      434312




      434312






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Try this:



          (nms <- names(read_excel("myexcelfile.xlsx")))
          (ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
          df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]

          df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")


          Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).






          share|improve this answer






















          • It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
            – Diogo Bastos
            Nov 9 at 23:00










          • I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
            – Diogo Bastos
            Nov 9 at 23:25










          • This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
            – arg0naut
            Nov 9 at 23:35










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234183%2fimporting-excel-file-with-read-excel-function-date-columns-is-not-correctly-imp%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Try this:



          (nms <- names(read_excel("myexcelfile.xlsx")))
          (ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
          df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]

          df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")


          Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).






          share|improve this answer






















          • It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
            – Diogo Bastos
            Nov 9 at 23:00










          • I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
            – Diogo Bastos
            Nov 9 at 23:25










          • This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
            – arg0naut
            Nov 9 at 23:35














          up vote
          1
          down vote













          Try this:



          (nms <- names(read_excel("myexcelfile.xlsx")))
          (ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
          df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]

          df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")


          Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).






          share|improve this answer






















          • It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
            – Diogo Bastos
            Nov 9 at 23:00










          • I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
            – Diogo Bastos
            Nov 9 at 23:25










          • This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
            – arg0naut
            Nov 9 at 23:35












          up vote
          1
          down vote










          up vote
          1
          down vote









          Try this:



          (nms <- names(read_excel("myexcelfile.xlsx")))
          (ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
          df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]

          df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")


          Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).






          share|improve this answer














          Try this:



          (nms <- names(read_excel("myexcelfile.xlsx")))
          (ct <- ifelse(grepl("^Date", nms), "date", "numeric"))
          df <- read_excel("myexcelfile.xlsx", col_types = ct)[-c(6:495),-c(3:71)]

          df$Date <- format(as.Date(as.character(df$Date)), "%d-%m-%Y")


          Instead of importing as text, import it as date and then format it in the way described below (there I suppose that you will save the Excel in the df dataframe and that the column will be called Date, but adjust as needed).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 23:33

























          answered Nov 9 at 22:53









          arg0naut

          1,592312




          1,592312











          • It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
            – Diogo Bastos
            Nov 9 at 23:00










          • I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
            – Diogo Bastos
            Nov 9 at 23:25










          • This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
            – arg0naut
            Nov 9 at 23:35
















          • It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
            – Diogo Bastos
            Nov 9 at 23:00










          • I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
            – Diogo Bastos
            Nov 9 at 23:25










          • This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
            – arg0naut
            Nov 9 at 23:35















          It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
          – Diogo Bastos
          Nov 9 at 23:00




          It doesnt work: I have this message: Error in charToDate(x) : string de caracteres não é um formato padrão não ambíguo
          – Diogo Bastos
          Nov 9 at 23:00












          I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
          – Diogo Bastos
          Nov 9 at 23:25




          I set arbitrarily: as.Date(df$Date,origin = "1899-12-30") and it works. Why?
          – Diogo Bastos
          Nov 9 at 23:25












          This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
          – arg0naut
          Nov 9 at 23:35




          This is the origin used by Excel. However I'm not sure why the approach above didn't work straight away, perhaps this is linked to different time zones.
          – arg0naut
          Nov 9 at 23:35

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234183%2fimporting-excel-file-with-read-excel-function-date-columns-is-not-correctly-imp%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ruanda

          Makov (Slowakei)

          Kleinkühnau