cast string directly to IDateTime

前端 未结 2 2018
予麋鹿
予麋鹿 2020-12-16 15:57

I am using the new version of data.table and especially the AWESOME fread function. My files contain dates that are loaded as strings (cause I don\

2条回答
  •  执笔经年
    2020-12-16 16:03

    I d'ont know how your file is structured, but from your comment you want to use the date field as a key. Why not to read it as a time series and format it when in reading?

    Here I use zoo to do it.(Here I suppose that the date column is the first one,otherwise see index.colum argument)

    ff <- function(x) as.POSIXct(strptime(x,"%d%b%Y:%H:%M:%S"))
    
    h <- read.zoo(text = "03avril2008:09:00:00  125
                          02avril2008:09:30:00  126
                          05avril2008:09:10:00  127
                          04avril2008:09:20:00  128
                          01avril2008:09:00:00  128"
                          ,FUN=ff)
    

    You get your dates sorted in the right format and sorted.

    The conversion is natural from POSIXct to IDateTime

        IDateTime(index(h))
            idate    itime
    1: 2008-04-01 09:00:00
    2: 2008-04-02 09:30:00
    3: 2008-04-03 09:00:00
    4: 2008-04-04 09:20:00
    5: 2008-04-05 09:10:00
    

    Here sure you still do 2 conversions, But you do it when reading data, and the second you do it without dealing with any format problem.

提交回复
热议问题