rdatetimeposixctterra

How to convert SpatVector column to datetime format in R?


I would like to convert a SpatVector column from character to POSIXct in R. Using as.POSIXct() doesn't throw an error or warning but the column is still character after.

f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)

v$DateTime <- "2022-03-02 01:23:45"
class(v) # class = character

v$DateTime <- as.POSIXct(v$DateTime)
class(v) # class = character

class(as.POSIXct(v$DateTime)) # class = POSIXct

Solution

  • This is available in terra >= 1.7-39

    library(terra)
    #terra 1.7.39
    f <- system.file("ex/lux.shp", package="terra")
    v <- vect(f)
    v$DateTime <- "2022-03-02 01:23:45"
    class(v$DateTime) 
    #[1] "character"
    
    v$DateTime <- as.POSIXct(v$DateTime)
    class(v$DateTime) 
    #[1] "POSIXct" "POSIXt"