从JSON请求插入嵌套列表中的字符

时间:2016-12-21 02:46:33

标签: json r foursquare rbind

我在RI中有以下列表mylist从运行函数jsonlite::rbind.pages()之前我尝试修改的foursquare api中回来,因为有一个嵌套的数据帧有0输入返回的值。当我尝试运行函数rbindpages时,它返回3个结果,而不是4个消除0值的数据帧。我如何将值插入到此中,以便当我使用rbind.pages()时,我会收到所有4个结果?目的是之后使用cbind()与其他向量合并。

这是控制台的输出:

[[1]]
                        id  name pluralName shortName                                          icon.prefix icon.suffix primary
1 4bf58dd8d48988d1fa931735 Hotel     Hotels     Hotel https://ss3.4sqi.net/img/categories_v2/travel/hotel_        .png    TRUE

[[2]]
                        id     name pluralName shortName                                            icon.prefix icon.suffix primary
1 4bf58dd8d48988d10f951735 Pharmacy Pharmacies  Pharmacy https://ss3.4sqi.net/img/categories_v2/shops/pharmacy_        .png    TRUE

[[3]]
data frame with 0 columns and 0 rows

[[4]]
                        id            name       pluralName       shortName                                              icon.prefix
1 52e81612bcbc57f1066b7a32 Cultural Center Cultural Centers Cultural Center https://ss3.4sqi.net/img/categories_v2/building/default_
  icon.suffix primary
1        .png    TRUE

这是dput:

mylist = list(structure(list(id = "4bf58dd8d48988d1fa931735", name = "Hotel", 
                    pluralName = "Hotels", shortName = "Hotel", icon = structure(list(
                      prefix = "https://ss3.4sqi.net/img/categories_v2/travel/hotel_", 
                      suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", "shortName", 
                                                "icon", "primary"), class = "data.frame", row.names = 1L), structure(list(
                                                  id = "4bf58dd8d48988d10f951735", name = "Pharmacy", pluralName = "Pharmacies", 
                                                  shortName = "Pharmacy", icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/shops/pharmacy_", 
                                                                                                suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                                                  primary = TRUE), .Names = c("id", "name", "pluralName", "shortName", 
                                                                              "icon", "primary"), class = "data.frame", row.names = 1L), structure(list(), .Names = character(0), row.names = integer(0), class = "data.frame"), 
     structure(list(id = "52e81612bcbc57f1066b7a32", name = "Cultural Center", 
                    pluralName = "Cultural Centers", shortName = "Cultural Center", 
                    icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/building/default_", 
                                          suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", 
                                                "shortName", "icon", "primary"), class = "data.frame", row.names = 1L), 
     structure(list(id = "4bf58dd8d48988d1e0931735", name = "Coffee Shop", 
                    pluralName = "Coffee Shops", shortName = "Coffee Shop", 
                    icon = structure(list(prefix = "https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_", 
                                          suffix = ".png"), .Names = c("prefix", "suffix"), class = "data.frame", row.names = 1L), 
                    primary = TRUE), .Names = c("id", "name", "pluralName", 
                                                "shortName", "icon", "primary"), class = "data.frame", row.names = 1L))

我注意到在寻找条件过滤器时,运行nchar(mylist[3])我得到了' 6'所以我的想法是运行类似下面的内容来查明" 0数据框"然后插入字符。只是一个概念,所以任何帮助都表示赞赏!

 mylist = lapply(mylist, function (x) {
       if (nchar(x) == 6) {

        x== data.frame(
           list(id = "4bf58dd8d48988d1e0931735", name = "Not avaliable", 
                pluralName = "Not avaliable", shortName = "Not avaliable", 
                icon = "https://ss3.4sqi.net/img/categories_v2/food", prefix = "https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_", 
                suffix = ".png")
         )
       } else {
         x
       }
     })

EDIT ************** 使用评论中的建议我得到以下内容:

[[11]]
[[11]][[1]]
                        id  name pluralName shortName                                          icon.prefix icon.suffix primary
1 4bf58dd8d48988d1fa931735 Hotel     Hotels     Hotel https://ss3.4sqi.net/img/categories_v2/travel/hotel_        .png    TRUE


[[12]]
NULL

[[13]]
[[13]][[1]]
                        id        name   pluralName   shortName                                              icon.prefix icon.suffix
1 52e81612bcbc57f1066b7a33 Social Club Social Clubs Social Club https://ss3.4sqi.net/img/categories_v2/building/default_        .png
  primary
1    TRUE

我跑了jsonlite::rbind.pages()并获得Error: all(vapply(pages, is.data.frame, logical(1))) is not TRUE

1 个答案:

答案 0 :(得分:0)

取消Conway所建议的内容,对他的回答进行了一些调整,并且确实有效:

  mylist = lapply(mylist, function (x) { 
    if (length(x) == 0) { 
      x<- data.frame( list(id = "00000000000000000",
                           name = "Not avaliable", 
                                pluralName = "Not avaliable",
                           shortName = "Not avaliable", 
                             icon.prefix = "https://ss3.4sqi.net/img/categories_v2/food", 
                           icon.suffix = ".png",
                           primary = "TRUE") ) 
      } else { 
        x <- x } })