Different Date Formats in R [duplicate]












0
















This question already has an answer here:




  • Convert dd/mm/yy and dd/mm/yyyy to Dates

    6 answers




In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").



Here I'm stuck. How can I get all of them to date format ?



Thank you










share|improve this question













marked as duplicate by Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 16:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Convert dd/mm/yy and dd/mm/yyyy to Dates

      6 answers




    In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").



    Here I'm stuck. How can I get all of them to date format ?



    Thank you










    share|improve this question













    marked as duplicate by Henrik r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 2 at 16:56


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Convert dd/mm/yy and dd/mm/yyyy to Dates

        6 answers




      In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").



      Here I'm stuck. How can I get all of them to date format ?



      Thank you










      share|improve this question















      This question already has an answer here:




      • Convert dd/mm/yy and dd/mm/yyyy to Dates

        6 answers




      In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").



      Here I'm stuck. How can I get all of them to date format ?



      Thank you





      This question already has an answer here:




      • Convert dd/mm/yy and dd/mm/yyyy to Dates

        6 answers








      r date






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 16:35









      CactusTownCactusTown

      1




      1




      marked as duplicate by Henrik r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 16:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Henrik r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 2 at 16:56


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          2














          An option would be parse_date_time from lubridate which can take multiple Date formats



          library(lubridate)
          parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
          #[1] "2017-11-14 UTC" "2016-04-14 UTC"




          Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with



          library(anytime)
          getFormats()


          If some formats are missing, add it with addFormats



          addFormats("%d-%b-%y")


          and then apply the anydate on the column/vector of dates



          anydate(v1)
          #[1] "2017-11-14" "2016-04-14"


          data



          v1 <- c("14-nov-17", "14APR2016")





          share|improve this answer



















          • 1





            Thank you so much it worked perfectly ! amazing package.

            – CactusTown
            Jan 3 at 8:55











          • @CactusTown Thank you for the comments. You can also check here

            – akrun
            Jan 3 at 8:56



















          0














          Another option, if you want to use base R and like regular expressions:



          data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
          format = "%d-%b-%y")





          share|improve this answer
























          • No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

            – Dirk Eddelbuettel
            Jan 2 at 16:49











          • @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

            – C. Braun
            Jan 2 at 17:07


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          An option would be parse_date_time from lubridate which can take multiple Date formats



          library(lubridate)
          parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
          #[1] "2017-11-14 UTC" "2016-04-14 UTC"




          Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with



          library(anytime)
          getFormats()


          If some formats are missing, add it with addFormats



          addFormats("%d-%b-%y")


          and then apply the anydate on the column/vector of dates



          anydate(v1)
          #[1] "2017-11-14" "2016-04-14"


          data



          v1 <- c("14-nov-17", "14APR2016")





          share|improve this answer



















          • 1





            Thank you so much it worked perfectly ! amazing package.

            – CactusTown
            Jan 3 at 8:55











          • @CactusTown Thank you for the comments. You can also check here

            – akrun
            Jan 3 at 8:56
















          2














          An option would be parse_date_time from lubridate which can take multiple Date formats



          library(lubridate)
          parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
          #[1] "2017-11-14 UTC" "2016-04-14 UTC"




          Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with



          library(anytime)
          getFormats()


          If some formats are missing, add it with addFormats



          addFormats("%d-%b-%y")


          and then apply the anydate on the column/vector of dates



          anydate(v1)
          #[1] "2017-11-14" "2016-04-14"


          data



          v1 <- c("14-nov-17", "14APR2016")





          share|improve this answer



















          • 1





            Thank you so much it worked perfectly ! amazing package.

            – CactusTown
            Jan 3 at 8:55











          • @CactusTown Thank you for the comments. You can also check here

            – akrun
            Jan 3 at 8:56














          2












          2








          2







          An option would be parse_date_time from lubridate which can take multiple Date formats



          library(lubridate)
          parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
          #[1] "2017-11-14 UTC" "2016-04-14 UTC"




          Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with



          library(anytime)
          getFormats()


          If some formats are missing, add it with addFormats



          addFormats("%d-%b-%y")


          and then apply the anydate on the column/vector of dates



          anydate(v1)
          #[1] "2017-11-14" "2016-04-14"


          data



          v1 <- c("14-nov-17", "14APR2016")





          share|improve this answer













          An option would be parse_date_time from lubridate which can take multiple Date formats



          library(lubridate)
          parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
          #[1] "2017-11-14 UTC" "2016-04-14 UTC"




          Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with



          library(anytime)
          getFormats()


          If some formats are missing, add it with addFormats



          addFormats("%d-%b-%y")


          and then apply the anydate on the column/vector of dates



          anydate(v1)
          #[1] "2017-11-14" "2016-04-14"


          data



          v1 <- c("14-nov-17", "14APR2016")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 16:37









          akrunakrun

          417k13205278




          417k13205278








          • 1





            Thank you so much it worked perfectly ! amazing package.

            – CactusTown
            Jan 3 at 8:55











          • @CactusTown Thank you for the comments. You can also check here

            – akrun
            Jan 3 at 8:56














          • 1





            Thank you so much it worked perfectly ! amazing package.

            – CactusTown
            Jan 3 at 8:55











          • @CactusTown Thank you for the comments. You can also check here

            – akrun
            Jan 3 at 8:56








          1




          1





          Thank you so much it worked perfectly ! amazing package.

          – CactusTown
          Jan 3 at 8:55





          Thank you so much it worked perfectly ! amazing package.

          – CactusTown
          Jan 3 at 8:55













          @CactusTown Thank you for the comments. You can also check here

          – akrun
          Jan 3 at 8:56





          @CactusTown Thank you for the comments. You can also check here

          – akrun
          Jan 3 at 8:56













          0














          Another option, if you want to use base R and like regular expressions:



          data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
          format = "%d-%b-%y")





          share|improve this answer
























          • No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

            – Dirk Eddelbuettel
            Jan 2 at 16:49











          • @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

            – C. Braun
            Jan 2 at 17:07
















          0














          Another option, if you want to use base R and like regular expressions:



          data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
          format = "%d-%b-%y")





          share|improve this answer
























          • No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

            – Dirk Eddelbuettel
            Jan 2 at 16:49











          • @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

            – C. Braun
            Jan 2 at 17:07














          0












          0








          0







          Another option, if you want to use base R and like regular expressions:



          data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
          format = "%d-%b-%y")





          share|improve this answer













          Another option, if you want to use base R and like regular expressions:



          data$date <- as.Date(sub('(\d{2})(\w{3})(\d{2})(\d{2})', '\1-\2-\4', data$date),
          format = "%d-%b-%y")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 16:44









          C. BraunC. Braun

          2,802629




          2,802629













          • No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

            – Dirk Eddelbuettel
            Jan 2 at 16:49











          • @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

            – C. Braun
            Jan 2 at 17:07



















          • No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

            – Dirk Eddelbuettel
            Jan 2 at 16:49











          • @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

            – C. Braun
            Jan 2 at 17:07

















          No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

          – Dirk Eddelbuettel
          Jan 2 at 16:49





          No, just say no. Never ever use regexps for data parsing when suitable libraries exist.

          – Dirk Eddelbuettel
          Jan 2 at 16:49













          @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

          – C. Braun
          Jan 2 at 17:07





          @DirkEddelbuettel I agree that 99% of the time (this case being one of them) that it is better to use a suitable library. Sometimes though I avoid libraries that help just one calculation if the code is being shared with people using different platforms, as package management and installation creates more dependencies and chances for things to go wrong. Would you argue that it would still be better for the sake of cleaner code?

          – C. Braun
          Jan 2 at 17:07



          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory