factor to date returns NA









up vote
-3
down vote

favorite
1












I am sorry but I struggle with this:



mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%M-%D")


it returns NA. Any ideas? Thanks!










share|improve this question





















  • Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
    – AntoniosK
    2 days ago











  • Similar answer here: [link]stackoverflow.com/questions/17496358/…
    – Jamie_D
    2 days ago










  • Look up format codes with ?strptime.
    – jay.sf
    2 days ago










  • um as.Date(mydate) works just fine since that's the default ISO format it expects
    – hrbrmstr
    2 days ago







  • 3




    Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
    – hrbrmstr
    2 days ago















up vote
-3
down vote

favorite
1












I am sorry but I struggle with this:



mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%M-%D")


it returns NA. Any ideas? Thanks!










share|improve this question





















  • Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
    – AntoniosK
    2 days ago











  • Similar answer here: [link]stackoverflow.com/questions/17496358/…
    – Jamie_D
    2 days ago










  • Look up format codes with ?strptime.
    – jay.sf
    2 days ago










  • um as.Date(mydate) works just fine since that's the default ISO format it expects
    – hrbrmstr
    2 days ago







  • 3




    Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
    – hrbrmstr
    2 days ago













up vote
-3
down vote

favorite
1









up vote
-3
down vote

favorite
1






1





I am sorry but I struggle with this:



mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%M-%D")


it returns NA. Any ideas? Thanks!










share|improve this question













I am sorry but I struggle with this:



mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%M-%D")


it returns NA. Any ideas? Thanks!







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









cs0815

5,2231975191




5,2231975191











  • Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
    – AntoniosK
    2 days ago











  • Similar answer here: [link]stackoverflow.com/questions/17496358/…
    – Jamie_D
    2 days ago










  • Look up format codes with ?strptime.
    – jay.sf
    2 days ago










  • um as.Date(mydate) works just fine since that's the default ISO format it expects
    – hrbrmstr
    2 days ago







  • 3




    Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
    – hrbrmstr
    2 days ago

















  • Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
    – AntoniosK
    2 days ago











  • Similar answer here: [link]stackoverflow.com/questions/17496358/…
    – Jamie_D
    2 days ago










  • Look up format codes with ?strptime.
    – jay.sf
    2 days ago










  • um as.Date(mydate) works just fine since that's the default ISO format it expects
    – hrbrmstr
    2 days ago







  • 3




    Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
    – hrbrmstr
    2 days ago
















Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
– AntoniosK
2 days ago





Try as.Date(mydate, format = "%Y-%m-%d"). Some more info on the formats here: statmethods.net/input/dates.html
– AntoniosK
2 days ago













Similar answer here: [link]stackoverflow.com/questions/17496358/…
– Jamie_D
2 days ago




Similar answer here: [link]stackoverflow.com/questions/17496358/…
– Jamie_D
2 days ago












Look up format codes with ?strptime.
– jay.sf
2 days ago




Look up format codes with ?strptime.
– jay.sf
2 days ago












um as.Date(mydate) works just fine since that's the default ISO format it expects
– hrbrmstr
2 days ago





um as.Date(mydate) works just fine since that's the default ISO format it expects
– hrbrmstr
2 days ago





3




3




Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
– hrbrmstr
2 days ago





Suggesting the use of lubridate for this example is crazy talk. It's the default ISO format and I don't think anyone rly wants to take 1250 microseconds to do the conversion (for one element) when the built-in as.Date() does it in 30 microseconds.
– hrbrmstr
2 days ago













2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").



mydate <- factor("2016-10-25")
as.Date(mydate, format = "%Y-%m-%d")

"2016-10-25"


enter image description here






share|improve this answer




















  • well I tried this - I think but yes this works now. Thanks!
    – cs0815
    2 days ago










  • You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
    – hrbrmstr
    2 days ago


















up vote
2
down vote













You can use lubridate as follows:



 mydate <- factor("2016-10-25")
require(lubridate)
ymd(mydate)





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',
    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%2f53225204%2ffactor-to-date-returns-na%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").



    mydate <- factor("2016-10-25")
    as.Date(mydate, format = "%Y-%m-%d")

    "2016-10-25"


    enter image description here






    share|improve this answer




















    • well I tried this - I think but yes this works now. Thanks!
      – cs0815
      2 days ago










    • You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
      – hrbrmstr
      2 days ago















    up vote
    3
    down vote



    accepted










    You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").



    mydate <- factor("2016-10-25")
    as.Date(mydate, format = "%Y-%m-%d")

    "2016-10-25"


    enter image description here






    share|improve this answer




















    • well I tried this - I think but yes this works now. Thanks!
      – cs0815
      2 days ago










    • You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
      – hrbrmstr
      2 days ago













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").



    mydate <- factor("2016-10-25")
    as.Date(mydate, format = "%Y-%m-%d")

    "2016-10-25"


    enter image description here






    share|improve this answer












    You need to use small letters for month and day ("%Y-%m-%d") instead of capital letters ("%Y-%M-%D").



    mydate <- factor("2016-10-25")
    as.Date(mydate, format = "%Y-%m-%d")

    "2016-10-25"


    enter image description here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    Can H.

    998




    998











    • well I tried this - I think but yes this works now. Thanks!
      – cs0815
      2 days ago










    • You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
      – hrbrmstr
      2 days ago

















    • well I tried this - I think but yes this works now. Thanks!
      – cs0815
      2 days ago










    • You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
      – hrbrmstr
      2 days ago
















    well I tried this - I think but yes this works now. Thanks!
    – cs0815
    2 days ago




    well I tried this - I think but yes this works now. Thanks!
    – cs0815
    2 days ago












    You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
    – hrbrmstr
    2 days ago





    You might want to also add the output of microbenchmark::microbenchmark(base = as.Date(mydate), lubr = lubridate::ymd(mydate)) to this answer to show the stark contrast to lubridate (also the format string is not necessary since it'll try the ISO format one by default)
    – hrbrmstr
    2 days ago













    up vote
    2
    down vote













    You can use lubridate as follows:



     mydate <- factor("2016-10-25")
    require(lubridate)
    ymd(mydate)





    share|improve this answer
























      up vote
      2
      down vote













      You can use lubridate as follows:



       mydate <- factor("2016-10-25")
      require(lubridate)
      ymd(mydate)





      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        You can use lubridate as follows:



         mydate <- factor("2016-10-25")
        require(lubridate)
        ymd(mydate)





        share|improve this answer












        You can use lubridate as follows:



         mydate <- factor("2016-10-25")
        require(lubridate)
        ymd(mydate)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        NelsonGon

        4819




        4819



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225204%2ffactor-to-date-returns-na%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20