0

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

  • Welcome to Stack Overflow. What packages are you using? Can you include some sample data so we can replicate the issue? – itsMeInMiami Sep 03 '20 at 22:44
  • Hi! So I found out which particular row was causing the issue: the latlon for this is (49.277298, -123.067902) and is the only one that shows the out of bounds message. – annieblabz Sep 04 '20 at 15:44

1 Answers1

0

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.

DanY
  • 5,920
  • 1
  • 13
  • 33
  • Ah! I've tried this. Unfortunately, when I did, it gave me "Error in parse_url(url) : length(url) == 1 is not TRUE" – annieblabz Sep 04 '20 at 15:03