how to convert factor into DateTime in R?

后端 未结 2 1645
臣服心动
臣服心动 2021-02-05 14:49

This question might be simple for some of you but bear with me since I\'m a beginner in R.

I have a dataframe that has a factor column (called time) containing DateTime

2条回答
  •  梦毁少年i
    2021-02-05 15:21

    Probably the easiest thing to do is use the lubridate packages which has a large number of functions for date manipulation. The following will convert your time into a POSIXct object:

     library(lubridate)
     mdy_hm( as.character(dataframe1$time) )
    

    See ?mdy to see the variety of date parsing functions.

    For a slightly more verbose version that does not rely on lubridate

    strptime(x = as.character( dataframe1$datetime ), format = "%d/%m/%Y %H:%M")
    

提交回复
热议问题