I am receiving a replacement error for my loop using tigris function in R

Hi, im new to R and Rcommunity.

m trying to get blockgroups for my dataset that has geocodes:

The issue I am facing is bizarely when my dataset has 1k rows I receive an output with a column for blockgroup but when I increase my dataset to 3k or more I do not receive any output of blockgroups and get the below errors of subscript out of bounds and replacement has less rows than data(see below)

Is there something wrong with my FOR loop?

setwd("C:/Users/bb/Desktop/ Study/Experimental")
> MyData <- read.csv(file="AddressMaster18k.csv", header=TRUE, sep=",")
> lat_tigris <- MyData$Latitude 
> lon_tigris <- MyData$Longitude
> blockgroup <- c()
> for ( i in 1:length(lat_tigris)){ 
+   block <- call_geolocator_latlon(lat_tigris[i], lon_tigris[i])
+   blockgroup[i] <- substr(block,1,nchar(block)-3)
+   #cat(paste(i," => ", blockgroup))
+ }
Error in response$result$geographies```
2010 Census Blocks`[[1]] : 
  subscript out of bounds
> MyData["blockgroup"] <- as.character(blockgroup)
Error in `[<-.data.frame`(`*tmp*`, "blockgroup", value = c("360810437012",  : 
  replacement has 668 rows, data has 6000
> head(MyData)
  AddressId   StreetAddressLine1         City ZipCode StateAbbreviation Latitude
1   6958258 58-19 Waldron Street       Corona   11368                NY 40.73910
2   6958302    1309 East Main St    Greenwich    6830                CT 41.54827
3   6958268 98607 Kilinoe Street         Aiea   96701                HI 21.39609
4   6958277    267 E. 7th Street     New York   10009                NY 40.72346
5   6958279    2444 Grassy Br Rd      Raccoon   41557                KY 37.51309
6   6958280   940 Nw Frecro Way  Jensen Beach   34957                FL 27.23904
   Longitude GeocodeSource MatchDescription
1  -73.85216        arcgis               NA
2  -73.01996        arcgis               NA
3 -157.93812        arcgis               NA
4  -73.97732        arcgis               NA
5  -82.41312        arcgis               NA
6  -80.24555        arcgis               NA


Maybe something inside your data. Can u upload your csv. I think it will be difficult to debug without the data

It gives error when u upload csv. it says file not authorized. only jpg,images etc permitted @Rodrigue


Print i inside your loop and show us rows around the position of i you have the error.

You can try also a minimal reprex data. see this :

Best Practices: how to prepare your own data for use in a reprex if you can’t, or don’t know how to reproduce a problem with a built-in dataset?

I figured one thing, my

> length(lat_tigris)
[1] 3000


> print( i )
[1] 669

i rprinted whats in my for loop and this part is giving error @Rodrigue

>   block <- call_geolocator_latlon(lat_tigris[i], lon_tigris[i])
Error in response$result$geographies$`2010 Census Blocks`[[1]] : 
  subscript out of bounds

these arent the same value. Is that what is causing the error? How does one resolve this?

what is the value of lat_tigris[i] and lon_tigris[i]?

[quote="Rodrigue, post:6, topic:42603"]

> lat_tigris[i]
[1] 22.74755
> lon_tigris[i]
[1] 81.91411

These two values are geocodes @Rodrigue

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.