Setup:
Setting up the Link:
paste(“https://maps.googleapis.com/maps/api/geocode/xml?”,”latlng=40.88925,-73.89858″,”&key=YOUR API KEY”,sep=””)
the above link will generate the following:
“https://maps.googleapis.com/maps/api/geocode/xml?latlng=40.88925,-73.89858&key=YOUR API KEY”
Now, to extract the XML data from google api service and bring it in RStudio we will use the readLines() function as follows:
link=paste(“https://maps.googleapis.com/maps/api/geocode/xml?”,”latlng=40.88925,-73.89858″,”&key=YOUR API KEY”,sep=””)
data = readLines(link)
head(data)
Looping:
sub= read.csv(“subway.csv”)
sub=na.omit
test=list()
for(i in 1:10){
test[i]= paste(“https://maps.googleapis.com/maps/api/geocode/xml?”,
“latlng=”,sub[i,3],”YOUR API KEY”,sep=””)
}
In the above mentioned code we set the the working directory using the setwd() function in R. The data is read in R using the read.csv() function. I usually use the na.omit() function to remove any NA in the data file. In the current data file we do not have any NA. But i like to be sure.
I have created a third column in the data file to make my life a bit easier. The third column uses the values from the first 2 columns separated by a comma. This third column will be used to generate the link. Google requires the latlng argument to be separated by a comma as in
latlng=40.88925,-73.89858
Finally the Loop:
In the loop below we start with an empty list test=list() before we execute the list. This is needed because when we run the loop R needs to store these values some where. In our case it is test.
for(i in 1:10){
test[i]= paste(“https://maps.googleapis.com/maps/api/geocode/xml?”,
“latlng=”,sub[i,3],”YOUR API KEY”,sep=””)
}
In the loop above R will substitute values 1 through 10 every time it comes across an i. The sub[i,3] is simply instructing R to go to the row i and column 3( as in sub[1,3], [2,3]…,[10,3]) and substitute the value in the paste function.
The whole process can easily be extended to all the rows in any file by simply using the following
test[i]= paste(“https://maps.googleapis.com/maps/api/geocode/xml?”,
“latlng=”,sub[i,3],”YOUR API KEY”,sep=””)
}
Conclusion:
The main objective of writing this post was to introduce new users of R to Looping in R, help them understand and use the paste() and readLines() function. In the next post we will use the extracted XML package in R to filter the list and extract the data we need.
Leave a Reply