I need to iterate over a couple thousand latitude and longitude pairs. I have written a for loop that only fills out the first 20 rows and then stops. I couldn't find any limit in the documentation, but I figured I could write a Sys.sleep
to get around it. No dice, and I must be missing something.
dput since I can't find a ready data source with lat/lon pairs.
> dput(fw %>% select(lat,lon, Offense) %>% head(22))
structure(list(lat = c(32.7305737101884, 32.7071796535597, 32.801343650586,
32.6497235905068, 32.9196968288275, 32.8213635863012, 32.7606031381985,
32.7090479791265, 32.7353753247409, 32.7785087695848, 32.7606031381985,
32.7268425544181, 32.7179541491143, 32.7500856566189, 32.7352690680259,
32.6608938090998, 32.741037616671, 32.7885499640296, 32.6267787210864,
32.6226562944531, 32.8036718521628, 32.7325453290282), lon = c(-97.3299315325775,
-97.440786931728, -97.3856410446521, -97.3216047378274, -97.3406980675876,
-97.4589971745759, -97.22609732855, -97.3480226360202, -97.4690739426462,
-97.3015112961882, -97.22609732855, -97.3316422324573, -97.439848768221,
-97.3569699397729, -97.471429159867, -97.4017441039168, -97.265145927386,
-97.3860506325934, -97.3798330465665, -97.3782646205598, -97.2906413984061,
-97.3701635007801), Offense = c("90Z", "23G", "220", "290", "220",
"90J", "90F", "290", "23H", "240", "290", "23C", "220", "90E",
"13B", "23H", "90Z", "23F", "13B", "23F", "35A", "120")), row.names = c(NA,
-22L), class = c("tbl_df", "tbl", "data.frame"))
Here is the code I've come up with so far:
fw$tract <- NA
for(i in seq_along(fw)){
fw$tract[i] <- tigris::call_geolocator_latlon(lat = fw$lat[i], lon = fw$lon[i])
if(i %% 20 == 0) {
Sys.sleep(1000)
}
}
Desired result: Data frame with tracts for more than just the first 20 rows.
Looking forward to any tips! Thanks!