Reputation: 1
library(ggmap)
library(tidyverse)
#create data frame for zipcode
data.new <- c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888,
20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490)
data.new <- data.frame(data.new)
data.new <- data.new %>% rename(zipcode=data.new)
data.new$zipcode <- as.character(data.new$zipcode)
register_google(key = "api_key")
data.new_ggmap <- geocode(location = data.new$zipcode, output = "latlona", source = "google")
data.new_ggmap <- cbind(data.new, data.new_ggmap)
#print
data.new_ggmaps[, 1:18]
Do any know how to fix this error? Note: I am working from home on personal pc
OPTION2
###OPTION2##############################################################
library(googleway)
library(tidyverse)
google_api_key <- "api_key"
#create data frame for zipcode
data.new <- c(21022, 20380, 22194, 20414, 20402, 22350, 20451, 56998, 88888,
20417, 75059, 90210, 11437, 10101, 10275, 10269, 10111, 98490)
data.new <- data.frame(data.new)
data.new <- data.new %>% rename(zipcode=data.new)
data.new$zipcode <- as.character(data.new$zipcode)
# initilize some columns to store the google data
data.new$goog_address_components_ <- NA
data.new$goog_formatted_address_ <- NA
data.new$goog_geometry_ <- NA
data.new$goog_place_id_ <- NA
data.new$goog_types_ <- NA
data.new$latitude <- NA
data.new$longitude <- NA
# loop through the rows of the data frame, passing the each zipcode of
# the trip to the google api each time
for(k in 1:nrow(data.new)){
# print the loop index to see the progress of the loop
print(k)
# wrap everything in a try catch statement so that if there is an error, it just
# skips the problem case and keeps going
tryCatch({
# define the search string by using the zipcode
search_string <- data.new$zipcode[k]
# pass the search string to the google way geocoding function, you
# have to have your own google API key to do this.
response<- google_geocode(search_string,
key = google_api_key)
# get the results from the api response
results_df <- response$results
# add the results from the api response to the existing data frame
data.new$goog_address_components_[k] <- results_df$address_components
data.new$goog_formatted_address_[k] <- results_df$formatted_address
data.new$goog_geometry_[k] <- results_df$geometry
data.new$goog_place_id_[k] <- results_df$place_id
data.new$goog_types_[k] <- results_df$types
}, error = function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
#########################################################################
I have the following error using googleway pakage
[1] 1
[1] 2
ERROR : replacement has 17 rows, data has 18
[1] 3
ERROR : replacement has 17 rows, data has 18
[1] 4
ERROR : replacement has 17 rows, data has 18
[1] 5
ERROR : replacement has 17 rows, data has 18
[1] 6
ERROR : replacement has 17 rows, data has 18
[1] 7
ERROR : replacement has 17 rows, data has 18
[1] 8
ERROR : replacement has 17 rows, data has 18
[1] 9
ERROR : replacement has 17 rows, data has 18
[1] 10
ERROR : replacement has 17 rows, data has 18
[1] 11
ERROR : replacement has 17 rows, data has 18
[1] 12
ERROR : replacement has 17 rows, data has 18
[1] 13
ERROR : replacement has 17 rows, data has 18
[1] 14
ERROR : replacement has 17 rows, data has 18
[1] 15
ERROR : replacement has 17 rows, data has 18
[1] 16
ERROR : replacement has 17 rows, data has 18
[1] 17
ERROR : replacement has 17 rows, data has 18
[1] 18
ERROR : replacement has 17 rows, data has 18
Warning message:
In data.new$goog_geometry_[k] <- results_df$geometry :
number of items to replace is not a multiple of replacement length
I have the following errors from the ggmaps package
Source : https://maps.googleapis.com/maps/api/geocode/json?address=21022&key=xxx-L8uKdnfgoejQBIQPFAo6lKAJLbU9E Source : https://maps.googleapis.com/maps/api/geocode/json?address=20380&key=xxx-L8uKdnfgoejQBIQPFAo6lKAJLbU9E Warning: Geocoding "20380" failed with error:
I try to geocode zipcodes to get lat and lon for each zipcodes
Upvotes: 0
Views: 39