I have used the walkscore API to generate output for a list of Lat and Longs
Reprex of dataset:
tibble::tribble(
~Lat, ~Long,
39.75454546, -82.63637088,
40.85117794, -81.47034464,
40.53956136, -74.33630685,
42.16066679, -71.21368025,
39.27048579, -119.5770782,
64.82534285, -147.6738774
)
My code:
library(walkscoreAPI)
library(rjson)
data = read.csv(file="geocode_finalcompiled.csv", header=TRUE, sep=",")
attach(data)
#create empty list
res = list()
# for loop through a file
for(i in 1:500){
res[i] = list(getWS(data$Long[i],data$Lat[i],"mykey"))
}
show results
res
> res
[[1]]
$status
[1] 1
$walkscore
[1] 2
$description
[1] "Car-Dependent"
$updated
[1] "2019-03-28 21:43:37.670012"
$snappedLong
[1] -82.6365
$snappedLat
[1] 39.7545
As you can see the output is in json format. My objective is to make this into a dataframe where each value is displayed under each header and can be put into a csv.
I tried:
resformatted <- as.data.frame(res)
But got the below error:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ‘"WalkScore"’ to a data.frame
What can be done to fix this?
list
notjson
or am I wrong? Unclass it and make it a df as required. – NelsonGonas.data.frame(unclass(res))
. Alternatively, what is the output ofsapply(res, class)
? – NelsonGon