I am retrieving data from an ArcGIS Rest service using a web request in an R script. One of the attribute columns does contain a date value. These dates are returned as epoch values (character strings). I try to convert these epoch strings to human readable dates, but until now to no avail...
See my reproducible script below. The column containing the date is called WVK_BEGDAT. The content of this column should be converted to human readable dates.
I have tried several suggestions found via Google...
library(httr)
library(sf)
library(lubridate)
url <- parse_url("https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services")
url$path <- paste(url$path, "NWB_Wegvakken/FeatureServer/0/query", sep = "/")
url$query <- list(where = "WEGBEHSRT = 'R' AND WEGNUMMER = '015'",
outFields = "*",
returnGeometry = "true",
f = "json")
request <- build_url(url)
request
NWB <- st_read(request, stringsAsFactors = FALSE)
plot(st_geometry(NWB))
NWB$WVK_BEGDAT2 <- as_date(NWB$WVK_BEGDAT, format="%d-%m-%Y", tz = "CET")
The closest I get is using the as_date()
function from the lubridate
package. This function at least does not return an error, bu fills the column with NAs.
Any suggestions? Thanks in advance.
as.POSIXct
should do the trick:
as.POSIXct(as.numeric(NWB$WVK_BEGDAT)/1000, origin = "1970-01-01")
You have to divide by 1000
, because your epoch times seem to be in milliseconds.