Convert spacetime object to spacetime cube
Source:R/spacetime-cube-complete.R
complete_spacetime_cube.Rd
Given a spacetime object, convert it to a spacetime cube. A spacetime cube ensures that there is a regular time-series for each geometry present.
Details
If observations are missing for a time period and location combination, columns will be populated with NAs.
See is_spacetime_cube()
for more details on spacetime cubes.
Examples
df_fp <- system.file("extdata", "bos-ecometric.csv", package = "sfdep")
geo_fp <- system.file("extdata", "bos-ecometric.geojson", package = "sfdep")
# read in data
df <- readr::read_csv(df_fp, col_types = "ccidD")
geo <- sf::read_sf(geo_fp)
# Create spacetime object called `bos`
bos <- spacetime(df, geo,
.loc_col = ".region_id",
.time_col = "time_period")
# create a sample of data
set.seed(0)
sample_index <- sample(1:nrow(bos), nrow(bos) * 0.95)
incomplete_spt <- bos[sample_index,]
# check to see if is spacetime cube
is_spacetime_cube(incomplete_spt)
#> ! Number of rows does not equal `n time-periods x n locations`
#> [1] FALSE
# complete it again
complete_spacetime_cube(incomplete_spt)
#> ! Vars(s) `ecometric`, `year`, and `value` are missing 84, 84, and 84 value(s).
#> spacetime ────
#> Context:`data`
#> 168 locations `.region_id`
#> 10 time periods `time_period`
#> ── data context ────────────────────────────────────────────────────────────────
#> # A tibble: 1,680 × 5
#> .region_id time_period ecometric year value
#> * <chr> <date> <chr> <int> <dbl>
#> 1 25025000100 2010-01-01 NA NA NA
#> 2 25025000100 2011-01-01 Guns 2011 1.54
#> 3 25025000100 2012-01-01 Guns 2012 1.84
#> 4 25025000100 2013-01-01 Guns 2013 4.36
#> 5 25025000100 2014-01-01 Guns 2014 1.91
#> 6 25025000100 2015-01-01 Guns 2015 1.93
#> 7 25025000100 2016-01-01 Guns 2016 1.5
#> 8 25025000100 2017-01-01 Guns 2017 2.14
#> 9 25025000100 2018-01-01 Guns 2018 2.78
#> 10 25025000100 2019-01-01 Guns 2019 1.69
#> # … with 1,670 more rows
#> # ℹ Use `print(n = ...)` to see more rows