I am attempting to add the Block FIP based on lat. long points to each row in my data frame, test, using this function I found here: r - Convert Lat/Lon to County Codes using FCC API - Stack Overflow
I make a new row in my data frame called tract and then loop through the data frame with the for loop.
geo2fips <- function(latitude, longitude) {
url <- "https://geo.fcc.gov/api/census/area?lat=%f&lon=%f&format=json"
res <- jsonlite::fromJSON(sprintf(url, latitude, longitude))[["results"]][["block_fips"]]
unique(res)
}
test$tract = rep(NA, nrow(test))
for (i in 1:nrow(test)) {
test$tract[i] = geo2fips(test$Latitude[i], test$Longitude[i])
}
However, this is the warning I get:
Warning messages:
1: In test$tract[i] <- geo2fips(test$Latitude[i], test$Longitude[i]) :
number of items to replace is not a multiple of replacement length
2: In test$tract[i] <- geo2fips(test$Latitude[i], test$Longitude[i]) :
number of items to replace is not a multiple of replacement length
Does anyone see what I am doing wrong? Also, is there an lapply or sapply way to do this that would be faster? My actual data set, not the test version, has 43k rows, so I need to be more efficient. Thanks!