-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin_nccs_block.R
148 lines (104 loc) · 2.91 KB
/
join_nccs_block.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
library( tigris )
library( tidyverse )
library( sf )
# set filepath of where to
# save raw block shapefiles
path <- "xwalk/"
#####
##### get shapefiles from tigris
#####
# set boolean for whether you want to download block shapefiles
download_block <- FALSE
# get unique state fips codes (including dc)
unique_states <- fips_codes %>%
filter(state_code != 74) %>%
pull(state_code) %>%
unique()
# only run if download_block is TRUE
if( download_block ){
# if shapefiles directory doesn't exist, create it
if( ! dir.exists(path) )
{ dir.create(path) }
##### download block shapefiles by state
##### abridge to only the geoid variable
##### and save
walk(unique_states, function(x) {
blocks(state = x,
year = 2010) %>%
select(GEOID10) %>%
st_write(str_c(path, "block_", x, ".geojson"))
})
} # end if( download_block )
# list paths of all
# saved block shapefiles
block_paths <-
list.files( path, full.names = TRUE )
# get state fips crosswalk
state_xwalk <-
fips_codes %>%
select( state_code, state ) %>%
distinct()
#####
##### read in nccs data
#####
nccs_data <- read_csv( str_c( path, "tinybmf.csv" ) )
nccs_data_sf <-
st_as_sf( nccs_data,
coords = c( "Longitude", "Latitude" ),
crs = 4326 ) %>%
select( id,
STATE,
Longitude,
Latitude ) %>%
st_transform( "EPSG:4269" )
# if intermediate directory doesn't exist, create it
if( !dir.exists("intermediate") )
{ dir.create("intermediate") }
# if final directory doesn't exist, create it
if( !dir.exists("final") )
{ dir.create("final") }
#####
##### joins nccs data to block data by state
#####
join_nccs <- function(my_state){
print(my_state)
# get state abbreviation
state_abbv <- state_xwalk %>%
filter(state_code == my_state) %>%
pull(state)
# keep only nccs data for the state we're joining to minimize memory and time
nccs_state <-
filter( nccs_data_sf,
STATE == state_abbv)
# read in block geojson
state_block <-
st_read(
str_c( path,
"block_",
my_state,
".geojson" ))
# join data and remove the geometry
data_out <-
st_join( nccs_state,
state_block ) %>%
st_drop_geometry()
# save the joined data
filename <- str_c( "intermediate/joined_block_", my_state, ".csv" )
write_csv( data_out, filename )
}
#####
##### iterate through all states and join data
#####
walk( unique_states, join_nccs)
# read in all joined data and append together
full_joined_dat <-
list.files( "intermediate",
pattern = "joined_block_",
full.names = TRUE ) %>%
map_dfr( ~read_csv( ., col_types = list(GEOID10 = col_character()) ))
# join abridged data with block info back to full dataset and write out
nccs_dat_full <-
left_join( nccs_data,
full_joined_dat,
by = c("id", "STATE") ) %>%
write_csv( "final/nccs_test.csv" )