convert Single digit day in R












1















I have dates in the format Apr42016, Aug12017, Apr112018. I am trying to convert in Y/m/d using R. I have tried the codes below but when I have a single digit for the day it returned NA. Anyone could help me, please?



strptime(data$date, "%b%e%Y")
as.Date (data$date, format="%b%d%Y")
as.POSIXct(data$date, format="%b%e%Y")


Thank you










share|improve this question





























    1















    I have dates in the format Apr42016, Aug12017, Apr112018. I am trying to convert in Y/m/d using R. I have tried the codes below but when I have a single digit for the day it returned NA. Anyone could help me, please?



    strptime(data$date, "%b%e%Y")
    as.Date (data$date, format="%b%d%Y")
    as.POSIXct(data$date, format="%b%e%Y")


    Thank you










    share|improve this question



























      1












      1








      1








      I have dates in the format Apr42016, Aug12017, Apr112018. I am trying to convert in Y/m/d using R. I have tried the codes below but when I have a single digit for the day it returned NA. Anyone could help me, please?



      strptime(data$date, "%b%e%Y")
      as.Date (data$date, format="%b%d%Y")
      as.POSIXct(data$date, format="%b%e%Y")


      Thank you










      share|improve this question
















      I have dates in the format Apr42016, Aug12017, Apr112018. I am trying to convert in Y/m/d using R. I have tried the codes below but when I have a single digit for the day it returned NA. Anyone could help me, please?



      strptime(data$date, "%b%e%Y")
      as.Date (data$date, format="%b%d%Y")
      as.POSIXct(data$date, format="%b%e%Y")


      Thank you







      r date format






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 19:15









      phiver

      13.2k92734




      13.2k92734










      asked Nov 13 '18 at 19:09









      user2386222user2386222

      345




      345
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You can modify the strings with sub (and add a 0 if necessary) before using as.Date:



          myvec <- c("Apr42016", "Aug12017", "Apr112018")  # the data

          myvec2 <- sub("(?<=[^0])(?=[0-9]{5})", "0", myvec, perl = TRUE)
          # [1] "Apr042016" "Aug012017" "Apr112018"

          as.Date(myvec2, format = "%b%d%Y")
          # [1] "2016-04-04" "2017-08-01" "2018-04-11"





          share|improve this answer































            2














            If you can break up the numbers before as.Date, it will make things much easier. (Borrowing Sven's look-behind.)



            sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2",
            c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
            # [1] "Apr-4-2016" "Aug-1-2017" "Apr-11-2018"


            From here, the format should be rather straight-forward:



            as.Date(sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2", c("Apr42016", "Aug12017", "Apr112018"), perl = TRUE),
            format="%b-%d-%Y")
            # [1] "2016-04-04" "2017-08-01" "2018-04-11"





            share|improve this answer

























              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',
              autoActivateHeartbeat: false,
              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%2f53287958%2fconvert-single-digit-day-in-r%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You can modify the strings with sub (and add a 0 if necessary) before using as.Date:



              myvec <- c("Apr42016", "Aug12017", "Apr112018")  # the data

              myvec2 <- sub("(?<=[^0])(?=[0-9]{5})", "0", myvec, perl = TRUE)
              # [1] "Apr042016" "Aug012017" "Apr112018"

              as.Date(myvec2, format = "%b%d%Y")
              # [1] "2016-04-04" "2017-08-01" "2018-04-11"





              share|improve this answer




























                2














                You can modify the strings with sub (and add a 0 if necessary) before using as.Date:



                myvec <- c("Apr42016", "Aug12017", "Apr112018")  # the data

                myvec2 <- sub("(?<=[^0])(?=[0-9]{5})", "0", myvec, perl = TRUE)
                # [1] "Apr042016" "Aug012017" "Apr112018"

                as.Date(myvec2, format = "%b%d%Y")
                # [1] "2016-04-04" "2017-08-01" "2018-04-11"





                share|improve this answer


























                  2












                  2








                  2







                  You can modify the strings with sub (and add a 0 if necessary) before using as.Date:



                  myvec <- c("Apr42016", "Aug12017", "Apr112018")  # the data

                  myvec2 <- sub("(?<=[^0])(?=[0-9]{5})", "0", myvec, perl = TRUE)
                  # [1] "Apr042016" "Aug012017" "Apr112018"

                  as.Date(myvec2, format = "%b%d%Y")
                  # [1] "2016-04-04" "2017-08-01" "2018-04-11"





                  share|improve this answer













                  You can modify the strings with sub (and add a 0 if necessary) before using as.Date:



                  myvec <- c("Apr42016", "Aug12017", "Apr112018")  # the data

                  myvec2 <- sub("(?<=[^0])(?=[0-9]{5})", "0", myvec, perl = TRUE)
                  # [1] "Apr042016" "Aug012017" "Apr112018"

                  as.Date(myvec2, format = "%b%d%Y")
                  # [1] "2016-04-04" "2017-08-01" "2018-04-11"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 19:20









                  Sven HohensteinSven Hohenstein

                  65.5k12100131




                  65.5k12100131

























                      2














                      If you can break up the numbers before as.Date, it will make things much easier. (Borrowing Sven's look-behind.)



                      sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2",
                      c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
                      # [1] "Apr-4-2016" "Aug-1-2017" "Apr-11-2018"


                      From here, the format should be rather straight-forward:



                      as.Date(sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2", c("Apr42016", "Aug12017", "Apr112018"), perl = TRUE),
                      format="%b-%d-%Y")
                      # [1] "2016-04-04" "2017-08-01" "2018-04-11"





                      share|improve this answer






























                        2














                        If you can break up the numbers before as.Date, it will make things much easier. (Borrowing Sven's look-behind.)



                        sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2",
                        c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
                        # [1] "Apr-4-2016" "Aug-1-2017" "Apr-11-2018"


                        From here, the format should be rather straight-forward:



                        as.Date(sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2", c("Apr42016", "Aug12017", "Apr112018"), perl = TRUE),
                        format="%b-%d-%Y")
                        # [1] "2016-04-04" "2017-08-01" "2018-04-11"





                        share|improve this answer




























                          2












                          2








                          2







                          If you can break up the numbers before as.Date, it will make things much easier. (Borrowing Sven's look-behind.)



                          sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2",
                          c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
                          # [1] "Apr-4-2016" "Aug-1-2017" "Apr-11-2018"


                          From here, the format should be rather straight-forward:



                          as.Date(sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2", c("Apr42016", "Aug12017", "Apr112018"), perl = TRUE),
                          format="%b-%d-%Y")
                          # [1] "2016-04-04" "2017-08-01" "2018-04-11"





                          share|improve this answer















                          If you can break up the numbers before as.Date, it will make things much easier. (Borrowing Sven's look-behind.)



                          sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2",
                          c("Apr42016", "Aug12017", "Apr112018"), perl=TRUE)
                          # [1] "Apr-4-2016" "Aug-1-2017" "Apr-11-2018"


                          From here, the format should be rather straight-forward:



                          as.Date(sub("(?<=\D)(\d+)(\d{4})$", "-\1-\2", c("Apr42016", "Aug12017", "Apr112018"), perl = TRUE),
                          format="%b-%d-%Y")
                          # [1] "2016-04-04" "2017-08-01" "2018-04-11"






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 13 '18 at 19:24

























                          answered Nov 13 '18 at 19:17









                          r2evansr2evans

                          26.3k33058




                          26.3k33058






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287958%2fconvert-single-digit-day-in-r%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

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues