Get State each site is entirely within, quickly, from table of blockid, ejam_uniq_id
Source:R/state_from_s2b_bysite.R
state_from_s2b_bysite.Rd
Find the 2-character State abbreviation, but only for sites entirely in 1 state.
Arguments
- sites2blocks
data.table or data.frame, like testoutput_getblocksnearby_10pts_1miles, from
getblocksnearby()
that has columns ejam_uniq_id and blockid and distance
Details
This only works for sites entirely in 1 state. For multistate sites, need lat/lon of site and that is slower. This returns NA for any site whose nearby blocks are in multiple states. For multistate sites, you need to know the lat/lon of the site to be sure of its state.
This function is for when you need to quickly find out the state each site is in, to be able to report state percentiles, This can identify the State each site is located in, based on the states of the nearby blocks (and parent blockgroups). In many analyses, all the sites will be single-state sites, and this function will be sufficient.
However, for the rare edge case where you did not save the lat,lon of sites you analyzed, you would need to approximate those from the lat/lon of the blocks and their distances, via latlon_from_s2b(), separately.
These two functions differ – one gets the state info for each unique site, and the other gets the state abbreviation of each unique block: Note: unexported function xx = state_from_s2b_bysite(testoutput_getblocksnearby_10pts_1miles)[] NROW(xx) 1 10 length(unique(testoutput_getblocksnearby_10pts_1miles$ejam_uniq_id)) 1 10
length(EJAM:::state_from_blockid_table(testoutput_getblocksnearby_10pts_1miles)) 1 1914 NROW(testoutput_getblocksnearby_10pts_1miles) 1 1914
Examples
if (FALSE) { # \dontrun{
# unexported function
table(state_from_blockid_table(testoutput_getblocksnearby_10pts_1miles))
state_from_s2b_bysite(testoutput_getblocksnearby_10pts_1miles)[]
x = getblocksnearby(pts, radius = 30)
y = state_from_s2b_bysite(x)
table(y$in_how_many_states)
y
fname = './inst/testdata/testpoints_207_sites_with_signif_violations_NAICS_326_ECHO.csv'
x = state_from_s2b_bysite(
getblocksnearby( latlon_from_anything(fname), quadtree = localtree))
y = read_csv_or_xl(fname)
x$ST == y$FacState
} # }
state_from_s2b_bysite(testoutput_getblocksnearby_10pts_1miles)