convert string date to R Date FAST for all dates

后端 未结 5 1945
天涯浪人
天涯浪人 2020-12-31 14:38

This has been asked several times with no clear answer: I would like to convert an R character string of the form \"YYYY-mm-dd\" into a Date. The as.Date<

5条回答
  •  Happy的楠姐
    2020-12-31 15:14

    I had a similar problem a while ago and came up with the following solution:

    1. convert the string to a factor (if not already a factor)
    2. convert the levels of the factor to a Date
    3. Expand the converted levels to the solution using the index vector of the factor

    Extending Joshua Ulrich's example, I get (with slower timings on my laptop)

    library(date)
    set.seed(21)
    x <- as.character(Sys.Date()-sample(40000, 1e6, TRUE))
    system.time(dDate <- as.Date(x))
    #    user  system elapsed 
    #    12.09   0.00   12.12 
    system.time(ddate <- as.Date(as.date(x,"ymd")))
    #    user  system elapsed 
    #    6.97    0.04    7.05 
    system.time({
        xf <- as.factor(x)
        dDate <- as.Date(levels(xf))[as.integer(xf)]
    })
    #    user  system elapsed 
    #    1.16    0.00    1.15
    

    Here, step 2 does not depend on the length of x once x is large enough and step 3 scales extremely well (simple vector indexing). The bottleneck should be step 1, which can be avoided if the data is already stored as a factor.

提交回复
热议问题