Skip to contents

Set a column from the geometry context of a spacetime object to the data context.

Usage

set_col(x, .from_geo, .to_data = .from_geo)

set_wts(x, .wt_col = "wt")

set_nbs(x, .nb_col = "nb")

Arguments

x

a spacetime object

.from_geo

the name of the column in the geometry context

.to_data

the name of the new variable to create in the data context

.wt_col

the name of the weights column in the geometry context

.nb_col

the name of neighbor column in the geometry context

Value

A spacetime object with an active data context and a new column from the geometry context.

Details

These functions will reorder the spacetime object to ensure that it is ordered correctly based on the location time columns in the geometry context defined by the loc_col and time_col attributes respectively.

set_wts() and set_nbs() create a new column in the data context with the same name as the column in the geometry context. If a different name is desired use set_col()

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")
bos <- activate(bos, "geometry")
bos$nb <- st_contiguity(bos)
bos$wt <- st_weights(bos$nb)
bos$card <- st_cardinalties(bos$nb)

set_nbs(bos)
#> spacetime ────
#> Context:`data`
#> 168 locations `.region_id`
#> 10 time periods `time_period`
#> ── data context ────────────────────────────────────────────────────────────────
#> # A tibble: 1,680 × 6
#>    .region_id  ecometric  year value time_period nb       
#>    <chr>       <chr>     <int> <dbl> <date>      <list>   
#>  1 25025010405 Guns       2010  0.35 2010-01-01  <int [8]>
#>  2 25025010404 Guns       2010  0    2010-01-01  <int [3]>
#>  3 25025010801 Guns       2010  0    2010-01-01  <int [4]>
#>  4 25025010702 Guns       2010  0.46 2010-01-01  <int [5]>
#>  5 25025010204 Guns       2010  0    2010-01-01  <int [3]>
#>  6 25025010802 Guns       2010  0    2010-01-01  <int [4]>
#>  7 25025010104 Guns       2010  0    2010-01-01  <int [5]>
#>  8 25025000703 Guns       2010  0    2010-01-01  <int [3]>
#>  9 25025000504 Guns       2010  0.22 2010-01-01  <int [5]>
#> 10 25025000704 Guns       2010  0    2010-01-01  <int [4]>
#> # … with 1,670 more rows
#> # ℹ Use `print(n = ...)` to see more rows
set_wts(bos)
#> spacetime ────
#> Context:`data`
#> 168 locations `.region_id`
#> 10 time periods `time_period`
#> ── data context ────────────────────────────────────────────────────────────────
#> # A tibble: 1,680 × 6
#>    .region_id  ecometric  year value time_period wt       
#>    <chr>       <chr>     <int> <dbl> <date>      <list>   
#>  1 25025010405 Guns       2010  0.35 2010-01-01  <dbl [8]>
#>  2 25025010404 Guns       2010  0    2010-01-01  <dbl [3]>
#>  3 25025010801 Guns       2010  0    2010-01-01  <dbl [4]>
#>  4 25025010702 Guns       2010  0.46 2010-01-01  <dbl [5]>
#>  5 25025010204 Guns       2010  0    2010-01-01  <dbl [3]>
#>  6 25025010802 Guns       2010  0    2010-01-01  <dbl [4]>
#>  7 25025010104 Guns       2010  0    2010-01-01  <dbl [5]>
#>  8 25025000703 Guns       2010  0    2010-01-01  <dbl [3]>
#>  9 25025000504 Guns       2010  0.22 2010-01-01  <dbl [5]>
#> 10 25025000704 Guns       2010  0    2010-01-01  <dbl [4]>
#> # … with 1,670 more rows
#> # ℹ Use `print(n = ...)` to see more rows
set_col(bos, "card")
#> spacetime ────
#> Context:`data`
#> 168 locations `.region_id`
#> 10 time periods `time_period`
#> ── data context ────────────────────────────────────────────────────────────────
#> # A tibble: 1,680 × 6
#>    .region_id  ecometric  year value time_period  card
#>    <chr>       <chr>     <int> <dbl> <date>      <int>
#>  1 25025010405 Guns       2010  0.35 2010-01-01      8
#>  2 25025010404 Guns       2010  0    2010-01-01      3
#>  3 25025010801 Guns       2010  0    2010-01-01      4
#>  4 25025010702 Guns       2010  0.46 2010-01-01      5
#>  5 25025010204 Guns       2010  0    2010-01-01      3
#>  6 25025010802 Guns       2010  0    2010-01-01      4
#>  7 25025010104 Guns       2010  0    2010-01-01      5
#>  8 25025000703 Guns       2010  0    2010-01-01      3
#>  9 25025000504 Guns       2010  0.22 2010-01-01      5
#> 10 25025000704 Guns       2010  0    2010-01-01      4
#> # … with 1,670 more rows
#> # ℹ Use `print(n = ...)` to see more rows
set_col(bos, "card", "cardinalities")
#> spacetime ────
#> Context:`data`
#> 168 locations `.region_id`
#> 10 time periods `time_period`
#> ── data context ────────────────────────────────────────────────────────────────
#> # A tibble: 1,680 × 6
#>    .region_id  ecometric  year value time_period cardinalities
#>    <chr>       <chr>     <int> <dbl> <date>              <int>
#>  1 25025010405 Guns       2010  0.35 2010-01-01              8
#>  2 25025010404 Guns       2010  0    2010-01-01              3
#>  3 25025010801 Guns       2010  0    2010-01-01              4
#>  4 25025010702 Guns       2010  0.46 2010-01-01              5
#>  5 25025010204 Guns       2010  0    2010-01-01              3
#>  6 25025010802 Guns       2010  0    2010-01-01              4
#>  7 25025010104 Guns       2010  0    2010-01-01              5
#>  8 25025000703 Guns       2010  0    2010-01-01              3
#>  9 25025000504 Guns       2010  0.22 2010-01-01              5
#> 10 25025000704 Guns       2010  0    2010-01-01              4
#> # … with 1,670 more rows
#> # ℹ Use `print(n = ...)` to see more rows