Я нашел следующий код для извлечения адресов улиц из файла данных и геокода с помощью Google API, однако, когда строка возвращает ошибку (Предупреждение: геокодирование «Адрес» завершилось ошибкой :), l oop останавливается и файл возвращается с долготой и широтой только к этой линии. Мне нужен какой-то тип оператора if, чтобы продолжить l oop, если API не возвращает результаты. Любая помощь будет принята с благодарностью, так как файл довольно большой.
# Select the file from the file chooser
fileToLoad <- file.choose(new = TRUE)
# Read in the CSV data and store it in a variable
origAddress <- read.csv(fileToLoad, stringsAsFactors = FALSE)
# Initialize the data frame
geocoded <- data.frame(stringsAsFactors = FALSE)
# Loop through the addresses to get the latitude and longitude of each address and add it to the
# origAddress data frame in new columns lat and lon
for(i in 1:nrow(origAddress))
{
# Print("Working...")
result <- geocode(origAddress$addresses[i], output = "latlona", source = "google")
origAddress$lon[i] <- as.numeric(result[1])
origAddress$lat[i] <- as.numeric(result[2])
origAddress$geoAddress[i] <- as.character(result[3])
}
# Write a CSV file containing origAddress to the working directory
write.csv(origAddress, "geocoded.csv", row.names=FALSE)