Add capital-to-capital distance to a data frame
Source:R/add_capital_distance.R
add_capital_distance.Rd
add_capital_distance()
allows you to add capital-to-capital
distance to a (dyad-year, leader-year, leader-dyad-year, state-year) data frame. The capitals are coded in the
cow_capitals
and gw_capitals
data frames, along with their latitudes and longitudes. The distance variable that
emerges capdist
is calculated using the "Vincenty" method (i.e. "as the crow
flies") and is expressed in kilometers.
Value
add_capital_distance()
takes a (dyad-year, leader-year, leader-dyad-year, state-year) data frame and adds the
capital-to-capital distance between the first state and the second state (in dyad-year data) or the minimum
capital-to-capital distance for a given state in a given year. A minor
note about this function: cases of capital transition are recorded in the
underlying data but, in the conversion to capital-years (and eventual
merging into a dyad-year data frame), the Jan. 1 capital is used for calculating
distances.
Details
The function leans on attributes of the data that are provided by one of the "create" functions
in this package (e.g. create_dyadyear()
or create_stateyear()
).
Examples
# \donttest{
# just call `library(tidyverse)` at the top of the your script
library(magrittr)
cow_ddy %>% add_capital_distance()
#> # A tibble: 2,139,270 × 4
#> ccode1 ccode2 year capdist
#> <dbl> <dbl> <int> <dbl>
#> 1 2 20 1920 2011.
#> 2 2 20 1921 2011.
#> 3 2 20 1922 2011.
#> 4 2 20 1923 2011.
#> 5 2 20 1924 2011.
#> 6 2 20 1925 2011.
#> 7 2 20 1926 2011.
#> 8 2 20 1927 2011.
#> 9 2 20 1928 2011.
#> 10 2 20 1929 2011.
#> # ℹ 2,139,260 more rows
create_stateyears() %>% add_capital_distance()
#> Warning: There were 390 warnings in `summarize()`.
#> The first warning was:
#> ℹ In argument: `mincapdist = min(.data$capdist, na.rm = TRUE)`.
#> ℹ In group 206: `ccode1 = 2`, `year = 2021`.
#> Caused by warning in `min()`:
#> ! no non-missing arguments to min; returning Inf
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 389 remaining warnings.
#> Joining with `by = join_by(ccode, year)`
#> # A tibble: 17,121 × 4
#> ccode statenme year mincapdist
#> <dbl> <chr> <int> <dbl>
#> 1 2 United States of America 1816 6677.
#> 2 2 United States of America 1817 6677.
#> 3 2 United States of America 1818 6677.
#> 4 2 United States of America 1819 6677.
#> 5 2 United States of America 1820 6677.
#> 6 2 United States of America 1821 6677.
#> 7 2 United States of America 1822 6679.
#> 8 2 United States of America 1823 6679.
#> 9 2 United States of America 1824 6679.
#> 10 2 United States of America 1825 6679.
#> # ℹ 17,111 more rows
# }