Converting characters to dates










-2















In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.



When I try to convert it to a date, I get this error:



> headlines$Date <- as.Date(headlines$Date)
Error in charToDate(x) :
character string is not in a standard unambiguous format


I've also tried the below, but get all NAs:



> headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")


How should I convert this column to dates?










share|improve this question


























    -2















    In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.



    When I try to convert it to a date, I get this error:



    > headlines$Date <- as.Date(headlines$Date)
    Error in charToDate(x) :
    character string is not in a standard unambiguous format


    I've also tried the below, but get all NAs:



    > headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")


    How should I convert this column to dates?










    share|improve this question
























      -2












      -2








      -2








      In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.



      When I try to convert it to a date, I get this error:



      > headlines$Date <- as.Date(headlines$Date)
      Error in charToDate(x) :
      character string is not in a standard unambiguous format


      I've also tried the below, but get all NAs:



      > headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")


      How should I convert this column to dates?










      share|improve this question














      In R, it seems like this should be obvious, but I'm having trouble. I have dates formatted as 1/1/00, 12/31/00, etc., where each part is abbreviated.



      When I try to convert it to a date, I get this error:



      > headlines$Date <- as.Date(headlines$Date)
      Error in charToDate(x) :
      character string is not in a standard unambiguous format


      I've also tried the below, but get all NAs:



      > headlines$Date <- as.Date(headlines$Date,format="%b/%d/%y")


      How should I convert this column to dates?







      r date






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 1:12









      Adam_GAdam_G

      2,302114898




      2,302114898






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.



          In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:



          headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")


          See this page for more info about date formats in R.






          share|improve this answer






























            1














            Replace format = "%b/%d/%y" with format = "%m/%d/%y".



            %b means month as in Jan, Feb, Mar and so on.



            %m is the integer equivalent (1, 2, 3 etc).



            Further reading: https://www.stat.berkeley.edu/~s133/dates.html






            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%2f53311069%2fconverting-characters-to-dates%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









              1














              You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.



              In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:



              headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")


              See this page for more info about date formats in R.






              share|improve this answer



























                1














                You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.



                In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:



                headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")


                See this page for more info about date formats in R.






                share|improve this answer

























                  1












                  1








                  1







                  You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.



                  In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:



                  headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")


                  See this page for more info about date formats in R.






                  share|improve this answer













                  You were on the right track by adding the format argument. I'm guessing you realized that the first error ("character string is not in a standard unambiguous format") happened because R doesn't know which of the numbers is the day, month, or the year. Say one of the values was "01/02/03"; there's no way of knowing whether it's 2 January 2003, or 1 February 2003, and so on.



                  In this case I think you just need to fix what you're passing to the format argument. %b is the symbol for abbreviated month in text form, not number form (e.g. "Jan" instead of "01"). You need to use %m instead for months stored as numbers. Try this:



                  headlines$Date <- as.Date(headlines$Date,format="%m/%d/%y")


                  See this page for more info about date formats in R.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 1:34









                  Boops BoopsBoops Boops

                  15114




                  15114























                      1














                      Replace format = "%b/%d/%y" with format = "%m/%d/%y".



                      %b means month as in Jan, Feb, Mar and so on.



                      %m is the integer equivalent (1, 2, 3 etc).



                      Further reading: https://www.stat.berkeley.edu/~s133/dates.html






                      share|improve this answer



























                        1














                        Replace format = "%b/%d/%y" with format = "%m/%d/%y".



                        %b means month as in Jan, Feb, Mar and so on.



                        %m is the integer equivalent (1, 2, 3 etc).



                        Further reading: https://www.stat.berkeley.edu/~s133/dates.html






                        share|improve this answer

























                          1












                          1








                          1







                          Replace format = "%b/%d/%y" with format = "%m/%d/%y".



                          %b means month as in Jan, Feb, Mar and so on.



                          %m is the integer equivalent (1, 2, 3 etc).



                          Further reading: https://www.stat.berkeley.edu/~s133/dates.html






                          share|improve this answer













                          Replace format = "%b/%d/%y" with format = "%m/%d/%y".



                          %b means month as in Jan, Feb, Mar and so on.



                          %m is the integer equivalent (1, 2, 3 etc).



                          Further reading: https://www.stat.berkeley.edu/~s133/dates.html







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 1:17









                          12b345b6b7812b345b6b78

                          792116




                          792116



























                              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%2f53311069%2fconverting-characters-to-dates%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

                              Use pre created SQLite database for Android project in kotlin

                              Darth Vader #20

                              Ondo