【问题标题】:filling in blank parameters for api list from JSON when using rbind.pages?使用 rbind.pages 时从 JSON 中填写 api 列表的空白参数?
【发布时间】:2016-11-09 19:21:04
【问题描述】:

在“搜索”部分使用foursquare API,以在给定经度和纬度点的特定地点提取数据。在拉动期间,有一个带有“类别”的部分给我带来了麻烦。我一直在使用我问过的上一个问题的建议: unest list with nested data frames in 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

我依赖 cbind,因为 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)

【问题讨论】:

    标签: json r api foursquare jsonlite


    【解决方案1】:

    类别是向量吗?你能用 NA 填充它吗?

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

    【讨论】:

    • 因此,这种运行程序的方式的问题在于,我发送的各种纬度和经度以及它们返回的类别向量的大小都是可变的,所以我不需要“30”以外的东西?
    猜你喜欢
    • 2023-04-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多