2. Creation and coercion

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  warning = FALSE,
  message = FALSE
)

library(cubble)

This article demonstrates how to create a cubble from various types of data. We will create a cubble from:

Create from separate spatial and temporal tables

In many cases, spatio-temporal data arrive in separate tables for analysis. For example, in climate data, analysts may initially receive station data containing geographic location information, recorded variables and their recording periods. They can then query the temporal variables using the stations of interest to obtain the relevant temporal data. Alternatively, analyses may begin as purely spatial or temporal, and analysts may obtain additional temporal or spatial data to expand the result to spatio-temporal.

The function make_cubble() composes a cubble object from a spatial table (spatial) and a temporal table (temporal), along with the three attributes key, index, and coords introduced in 1. The cubble class. The following code creates the nested cubble:

make_cubble(spatial = stations, temporal = meteo,
            key = id, index = date, coords = c(long, lat))

The coords argument can be safely omitted if the spatial data is an sf object (e.g. stations_sf) . Similarly, if the temporal object is a tsibble (i.e. meteo_ts), you don't need to specify the key and index arguments. The class attributes from sf and tsibble will be carried over to the nested and long cubble:

(res <- make_cubble(spatial = stations_sf, temporal = meteo_ts))
class(res)
class(res$ts[[1]])

The vignette 3. Compatibility with tsibble and sf will introduce more on the cubble's compatibility with tsibble and sf.

Coerce from foreign objects

The tibble objects

The dataset climate_flat combines the spatial data, stations, with the temporal data, meteo, into a single tibble object. It can be coerced into a cubble using:

climate_flat %>% as_cubble(key = id, index = date, coords = c(long, lat))

The NetCDF data

In R, there are several packages available for wrangling NetCDF data, including ncdf4, RNetCDF, and tidync. The code below converts a NetCDF object of class ncdf4 into a cubble object:

path <- system.file("ncdf/era5-pressure.nc", package = "cubble")
raw <- ncdf4::nc_open(path)
as_cubble(raw)

Sometimes, analysts may choose to read only a subset of the NetCDF data. In such cases, the vars, long_range and lat_range arguments can be used to subset the data based on the variable and the grid resolution:

as_cubble(raw, vars = "q",
          long_range = seq(-180, 180, 1), lat_range = seq(-90, 90, 1))

The stars objects

tif <- system.file("tif/L7_ETMs.tif", package = "stars")
x <- stars::read_stars(tif)
x %>% as_cubble()

When the dimensions object is too complex for the cubble package to handle, a warning message will be generated.

The sftime objects

dt <- climate_flat %>%
  sf::st_as_sf(coords = c("long", "lat"), crs = sf::st_crs("OGC:CRS84")) %>%
  sftime::st_as_sftime()
dt %>% as_cubble(key = id, index = date)


Try the cubble package in your browser

Any scripts or data that you put into this service are public.

cubble documentation built on July 9, 2023, 6:19 p.m.