How to convert a string factor into Date?

前端 未结 5 1051
情话喂你
情话喂你 2021-01-24 17:52

I\'m working with the London crime dataset which contains dates in an integer format. I melted them and now they have become factors.

For example, \"

5条回答
  •  自闭症患者
    2021-01-24 18:13

    Using anytime:

    test=as.factor("X200801")
    library(anytime)
    anydate((gsub("X","",test)))
    [1] "2008-01-01"
    

    As suggested by @akrun we can simply supply a format that will be used within our environment with addFormats and use anydate to convert to date:

    addFormats("X%Y%m")
    anydate("X200801")
    

提交回复
热议问题