2016-11-09 51 views
1

取消「搜索」部分的foursquare API以在給定經度和緯度點的特定位置上提取數據。在拉有一款與「類別這是給我的麻煩,我一直使用的建議從以前的問題,我問: unest list with nested data frames in R使用rbind.pages填充JSON的api列表的空白參數?

但是,用的是‘探索’從FS其中API它完美地工作現在我使用「搜索」 API和一些LAT長點都在嵌套數據幀拉不存在類別:

     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 

我對cbind依賴,因爲ID是不我如何解釋這些零點,並將類別作爲一個「不適用」來構建,這樣,當我綁定時,我不會得到以下錯誤?實質上,不存在的函數nction是殺害零數據幀,縮短了我的名單綁定

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) 

回答

0

是類別向量?你能用NA填寫嗎?

if (length(categories) < 30) {categories <- c(categories, rep(NA, 30 - length(categories)))} 
+0

因此,與運行程序這種方式的問題是,即時通訊在各種拉特,長,他們返回的類別向量的大小可變,所以也不會,我需要其他則「30」發送的東西? – LoF10