Converting a character string into a date in R

前端 未结 2 728
离开以前
离开以前 2020-12-06 21:35

The data I\'m trying to convert is supposed to be a date, however it is formatted as mmddyyyy with no separation by dashes or slashes. In order to work with dates in R, I wo

2条回答
  •  佛祖请我去吃肉
    2020-12-06 21:55

    Have a look at lubridate mdy function

    require(lubridate)
    a <- "10281994"
    mdy(a)
    

    gives you

    [1] "1994-10-28 UTC"
    

    of class "POSIXct" "POSIXt" so a datetime in R. (thanks Joshua Ulrich for the correction)

    You could use as.Date(mdy(a)) = 1994-10-28 to get a Object of class Date.

    There are mutations like ymd and dmy within lubridate as well.

提交回复
热议问题