how to convert factor into DateTime in R?

后端 未结 2 1644
臣服心动
臣服心动 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条回答
  • 2021-02-05 15:18

    Try:

    dataframe1$datetime <- strptime(x = as.character(dataframe1$datetime),
                                    format = "%d/%m/%Y %H:%M")
    
    0 讨论(0)
  • 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")
    
    0 讨论(0)
提交回复
热议问题