Update: so in my dataset, one of the coordinate sets is (49.277298, -123.067902), and is not only the only one out of bounds, but also the only one wreaking havoc for the analysis. Hope this addition helps!

I'm working on finding census codes for a dataset that only lists the latitudes and longitudes but keep getting Error in response$result$geographies$2010 Census Blocks[[1]] : subscript out of bounds

When I use a for loop:

ids <- c()
for(i in 1:nrow(census)){
if(is.na(census$locationlatitude[i])){
id <- NA
} else {
id <- call_geolocator_latlon(census$locationlatitude[i], census$locationlongitude[i])
}
ids <- c(ids, (id))
}

And then I also use an apply:

census <- read.csv("census_tract.csv")
census <- census[,c(1:3)]
census$rowid <- 1:nrow(census)
censusNA <- na.omit(census)
censusNA$census_tract <- apply(censusNA, 1, function(row) 
call_geolocator_latlon(row['LocationLatitude'], row['LocationLongitude']))
censusNA <- censusNA[,c(3,4)]
census_tract <- merge(census, censusNA, by = "rowid", all.x = TRUE)

both return Error in response$result$geographies$2010 Census Blocks[[1]] : subscript out of bounds

1

There are 1 answers

1
DanY On BEST ANSWER

You should be able to avoid the loop by using ifelse() as in:

ifelse( is.na(census$locationlatitude),  
        NA, 
        call_geolocator_latlon(census$locationlatitude, census$locationlongitude)
       )

This assumes call_geolocator_latlon is vectorized.