在"search"部分使用foursquare API提取给定经度和纬度点的特定地点的数据。在拉的过程中,有一个"类别"的部分给我带来了麻烦。我一直在使用我之前问过的一个问题的建议:在R
中嵌套数据帧的列表然而,这是使用FS的"探索"API,它工作得很好。现在我正在使用"搜索"API,一些后期长点正在拉入没有类别的嵌套数据框架:
id name pluralName
1 4bf58dd8d48988d1c1941735 Mexican Restaurant Mexican Restaurants
shortName icon.prefix
1 Mexican https://ss3.4sqi.net/img/categories_v2/food/mexican_
icon.suffix primary
1 .png TRUE
[[14]]
**data frame with 0 columns and 0 rows**
[[15]]
id name pluralName shortName
1 4bf58dd8d48988d11b951735 Flower Shop Flower Shops Flower Shop
icon.prefix icon.suffix
1 https://ss3.4sqi.net/img/categories_v2/shops/flowershop_ .png
primary
1 TRUE
我依赖于中国,因为id不兼容合并。我如何解释这些零,并将类别构建为"NA",以便当我绑定时,我不会得到以下错误?从本质上讲,取消列表函数杀死了零数据帧,缩短了列表以绑定
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 30, 29
下面是我的代码:
foursquare<-function(x,y,z,r){
w<-paste("https://api.foursquare.com/v2/venues/search?ll=",x,
"&radius=",r,"&oauth_token=",y,"&v=",z,sep="")
u<-getURL(w)
test<-fromJSON(u)
{locationid =""
locationname=""
location =""
lat=""
long=""
categories = ""
checkinscount = ""
userscount = ""
beenhere=""
herenow=""}
for(n in 1:length(test$response$venues)) {
#extract
locationid = test$response$venues$id
locationname = test$response$venues$name
location= test$response$venues$location$address
lat = test$response$venues$location$lat
long = test$response$venues$location$lng
categories= test$response$venues$categories
checkinscount = test$response$venues$stats$checkinsCount
userscount = test$response$venues$stats$usersCount
beenhere = test$response$venues$beenHere$unconfirmedCount
herenow = test$response$venues$hereNow$count
search_api = as.data.frame(cbind(locationid, locationname, location, lat, long,
checkinscount,userscount, beenhere, herenow))
print(categories)
categories = jsonlite::rbind.pages(categories)
categories = categories[, c("name")]
print(categories)
search_api = as.data.frame(cbind(search_api, categories))
}
#add columns
search_api$pulled_date = Sys.time()
search_api$x_query = paste(x)
search_api$y_query = y
search_api$type_api = 'search'
search_api$radius = paste(r)
#prep for writeout
time = gsub("[[:punct:]]", "", Sys.time())
filename <- paste(time,"search_api",".csv", sep="")
print(filename)
write.csv(search_api, file = filename)
}
foursquare("40.7575425406984,-73.9295267264121","auth_tok","20161027", 1000)
categories是一个向量吗?你能把它装上NA吗?
if (length(categories) < 30) {categories <- c(categories, rep(NA, 30 - length(categories)))}