How to convert dd/mm/yy to yyyy-mm-dd in R

后端 未结 6 2132
日久生厌
日久生厌 2020-12-20 06:16

I have a vector with dates values in this format dd/mm/yy e.g.(27/06/16). I want to convert this in this format yyyy-mm-dd e.g.(2016-06-27) for logical comparison. I am usin

相关标签:
6条回答
  • 2020-12-20 06:48

    Try

    as.Date( as.character("27/06/16"), format = "%Y-%m-%d")
    
    0 讨论(0)
  • 2020-12-20 06:50

    One option would be to convert both date formats into a common POSIX representation:

    d1 <- strptime("27/06/16", "%d/%m/%y")
    d2 <- strptime("2016-06-27", "%Y-%m-%d")
    

    Since both d1 and d2 should be of the same R class, you can then just compare them as you wish.

    0 讨论(0)
  • 2020-12-20 06:50
    strDates <- c("27/06/16")
    dates <- as.Date(strDates, "%m/%d/%Y")
    

    this works.

    refer this for more.http://www.statmethods.net/input/dates.html

    0 讨论(0)
  • 2020-12-20 06:55

    Use lubridate package

    library(lubridate)
    dmy("27/06/16")
    
    0 讨论(0)
  • 2020-12-20 07:03

    Using dplyr

    df.with.converted.date <- df.with.original.date %>%
      mutate(new.date.var = as.character(as.Date(old.date.var, "%m/%d/%Y"),"%Y%m%d"))
    

    For example this will convert "5/3/2019" to 20190503

    0 讨论(0)
  • 2020-12-20 07:11

    With base R

    as.Date(as.character("27/06/16"), format = "%d/%m/%y")
    
    0 讨论(0)
提交回复
热议问题