The following is a script file containing all R code of all sections in this slide set.

R and Spatio-Temporal Data

library(xts)
library(quantmod)
gold <- getMetals("XAU",from=Sys.Date()-90,auto.assign=FALSE)
head(gold)
gold["2019-12-03"]
gold["2020-01-02/2020-01-05"]
someDates <- seq.Date(as.Date("2014-07-20"),by="day",length=10)
someDates
someValues <- rnorm(10)
someValues
theObj <- xts(someValues,someDates)
theObj[1:3]
plot(theObj)
plot(gold,main="Prices of Gold")
library(readr)
df <- read_csv("firesnew_25000_500m.txt")
df[1:3,]
library(sp)
spatialCoords <- cbind(long=df$x, lat=df$y)
coordRefSys <- CRS("+proj=longlat +ellps=WGS84")
fires2000 <- SpatialPointsDataFrame(spatialCoords,
                                    df[, "ano2000", drop=FALSE],
                                    proj4string=coordRefSys)
fires2000[1:3,]
bbox(fires2000)
coordinates(fires2000)[1:3,]
summary(fires2000)
library(readr)
df <- read_csv("firesnew_25000_500m.txt")
df[1:3,]
library(tidyr)
x <- gather(df[,3:14], year, burnt, ano1991:ano2000)
x[1:2,]
x$year <- substr(x$year,4,7)
x[1:2,]
library(sp)
library(spacetime)
spatialCoords <- cbind(long=df$x,lat=df$y)
coordRefSys <- CRS("+proj=longlat +ellps=WGS84")
## The spatial points for which we have information (burnt or not)
sp <- SpatialPoints(spatialCoords,coordRefSys)
## The time stamps for which we have information (for all points - full grid)
timeStamps <- as.POSIXct(paste0(unique(x$year),"-01-01"), tz="GMT")
std <- STFDF(sp,timeStamps,data=x[,"burnt",drop=FALSE])
## The first location (spatial point)
std[1, ] 
## Second position is a temporal query on all locations
dataSince1995 <- std[,"1995/"]
data1991 <- std[,"1991"]
## And these are the values for the first 4 locations for 1991
data1991[1:4,]