0

我有一个名为 ct2 的列表来自网络抓取。我可以使用以下代码命名非嵌套列表中的列。但是我想添加坐标,当我调用head(ct2$business)时,我看到ct2$business$coordinates$latitudect2$business$coordinates$longitude。我似乎无法在没有收到错误的情况下将它们拉到数据框中。所以我不确定我做错了什么。下面是用于分配列的代码。以下是我尝试添加坐标时收到的错误。

biz_info <- ct2$businesses %>% 
map_df(`[`, c("name", "id", "phone", "review_count","rating","url")) 
biz_info %>% knitr::kable()

当我添加坐标时,尝试执行时出现错误

biz_info <- ct2$businesses %>% 
map_df(`[`, c("name", "id", "phone", 
"review_count","rating","url","coordinates")) 
Error in bind_rows_(x, .id) : Argument 7 must be length 1, not 2
biz_info %>% knitr::kable()

**编辑以添加数据示例

dput(head(ct2$businesses))
list(structure(list(id = "mcdonalds-hartford-7", name = "McDonald's", 
image_url = "https://s3-
media3.fl.yelpcdn.com/bphoto/hgpL9l7A10vRoWy84NPV_g/o.jpg", 
is_closed = FALSE, url = "https://www.yelp.com/biz/mcdonalds-hartford-7?", 
review_count = 4L, categories = list(structure(list(alias = "hotdogs", 
    title = "Fast Food"), .Names = c("alias", "title")), 
    structure(list(alias = "burgers", title = "Burgers"), .Names = c("alias", 
    "title"))), rating = 3.5, coordinates = structure(list(
    latitude = 41.738611, longitude = -72.65921), .Names = c("latitude", 
"longitude")), transactions = list(), price = "$", location = structure(list(
    address1 = "76 Brainard Rd", address2 = "", address3 = "", 
    city = "Hartford", zip_code = "06114", country = "US", 
    state = "CT", display_address = list("76 Brainard Rd", 
        "Hartford, CT 06114")), .Names = c("address1", "address2", 
"address3", "city", "zip_code", "country", "state", "display_address"
)), phone = "+18602477300", display_phone = "(860) 247-7300", 
distance = 3155.923625766), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "price", "location", "phone", "display_phone", 
"distance")), structure(list(id = "mcdonalds-restaurants-hartford-3", 
name = "McDonalds Restaurants", image_url = "", is_closed = FALSE, 
url = "https://www.yelp.com/biz/mcdonalds-restaurants-hartford-3?", 
review_count = 2L, categories = list(structure(list(alias = "restaurants", 
    title = "Restaurants"), .Names = c("alias", "title"))), 
rating = 2.5, coordinates = structure(list(latitude = 41.75251, 
    longitude = -72.71448), .Names = c("latitude", "longitude"
)), transactions = list(), location = structure(list(address1 = "214 Prospect Ave", 
    address2 = "", address3 = "", city = "Hartford", zip_code = "06106", 
    country = "US", state = "CT", display_address = list(
        "214 Prospect Ave", "Hartford, CT 06106")), .Names = c("address1", 
"address2", "address3", "city", "zip_code", "country", "state", 
"display_address")), phone = "+18605238859", display_phone = "(860) 523-8859", 
distance = 2591.628349648), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "location", "phone", "display_phone", "distance"
)), structure(list(id = "mcdonalds-hartford-9", name = "McDonald's", 
image_url = "https://s3-media4.fl.yelpcdn.com/bphoto/49EjiRF2Yb91rBV6wbuHZw/o.jpg", 
is_closed = FALSE, url = "https://www.yelp.com/biz/mcdonalds-hartford-9?", 
review_count = 9L, categories = list(structure(list(alias = "burgers", 
    title = "Burgers"), .Names = c("alias", "title")), structure(list(
    alias = "hotdogs", title = "Fast Food"), .Names = c("alias", 
"title"))), rating = 2.5, coordinates = structure(list(latitude = 41.75251, 
    longitude = -72.71448), .Names = c("latitude", "longitude"
)), transactions = list(), price = "$", location = structure(list(
    address1 = "214 Prospect Ave", address2 = "", address3 = "", 
    city = "Hartford", zip_code = "06106", country = "US", 
    state = "CT", display_address = list("214 Prospect Ave", 
        "Hartford, CT 06106")), .Names = c("address1", "address2", 
"address3", "city", "zip_code", "country", "state", "display_address"
)), phone = "+18605235303", display_phone = "(860) 523-5303", 
distance = 2591.628349648), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "price", "location", "phone", "display_phone", 
"distance")), structure(list(id = "mcdonalds-hartford-10", name = "McDonald's", 
image_url = "https://s3-media4.fl.yelpcdn.com/bphoto/da-sL4n1xX2VkLbqIWr5hw/o.jpg", 
is_closed = FALSE, url = "https://www.yelp.com/biz/mcdonalds-hartford-10?", 
review_count = 9L, categories = list(structure(list(alias = "burgers", 
    title = "Burgers"), .Names = c("alias", "title")), structure(list(
    alias = "hotdogs", title = "Fast Food"), .Names = c("alias", 
"title"))), rating = 1, coordinates = structure(list(latitude = 41.7573503, 
    longitude = -72.68223), .Names = c("latitude", "longitude"
)), transactions = list(), location = structure(list(address1 = "172 Washington St", 
    address2 = "", address3 = "", city = "Hartford", zip_code = "06106", 
    country = "US", state = "CT", display_address = list(
        "172 Washington St", "Hartford, CT 06106")), .Names = c("address1", 
"address2", "address3", "city", "zip_code", "country", "state", 
"display_address")), phone = "+18605602292", display_phone = "(860) 560-2292", 
distance = 374.2938759334), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "location", "phone", "display_phone", "distance"
)), structure(list(id = "mcdonalds-hartford-12", name = "McDonald's", 
image_url = "https://s3-media4.fl.yelpcdn.com/bphoto/B0SDIM3ylqAN6hOgOkyybQ/o.jpg", 
is_closed = FALSE, url = "https://www.yelp.com/biz/mcdonalds-hartford-12", 
review_count = 4L, categories = list(structure(list(alias = "hotdogs", 
    title = "Fast Food"), .Names = c("alias", "title")), 
    structure(list(alias = "burgers", title = "Burgers"), .Names = c("alias", 
    "title"))), rating = 2, coordinates = structure(list(
    latitude = 41.7828446485687, longitude = -72.6981766090747), .Names = c("latitude", 
"longitude")), transactions = list(), price = "$", location = structure(list(
    address1 = "1303 Albany Ave", address2 = "", address3 = "", 
    city = "Hartford", zip_code = "06112", country = "US", 
    state = "CT", display_address = list("1303 Albany Ave", 
        "Hartford, CT 06112")), .Names = c("address1", "address2", 
"address3", "city", "zip_code", "country", "state", "display_address"
)), phone = "+18602473612", display_phone = "(860) 247-3612", 
distance = 2730.544003504), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "price", "location", "phone", "display_phone", 
"distance")), structure(list(id = "mcdonalds-hartford", name = "McDonald's", 
image_url = "https://s3-media2.fl.yelpcdn.com/bphoto/rnWHncxwC1qK5T9KvSIVBA/o.jpg", 
is_closed = FALSE, url = "https://www.yelp.com/biz/mcdonalds-hartford?", 
review_count = 8L, categories = list(structure(list(alias = "hotdogs", 
    title = "Fast Food"), .Names = c("alias", "title")), 
    structure(list(alias = "burgers", title = "Burgers"), .Names = c("alias", 
    "title"))), rating = 1.5, coordinates = structure(list(
    latitude = 41.7876, longitude = -72.66214), .Names = c("latitude", 
"longitude")), transactions = list(), price = "$", location = structure(list(
    address1 = "98 Weston St", address2 = "", address3 = "", 
    city = "Hartford", zip_code = "06120", country = "US", 
    state = "CT", display_address = list("98 Weston St", 
        "Hartford, CT 06120")), .Names = c("address1", "address2", 
"address3", "city", "zip_code", "country", "state", "display_address"
)), phone = "+18607240200", display_phone = "(860) 724-0200", 
distance = 3622.578151942), .Names = c("id", "name", "image_url", 
"is_closed", "url", "review_count", "categories", "rating", "coordinates", 
"transactions", "price", "location", "phone", "display_phone", 
"distance")))
4

1 回答 1

1

Argh 进行了很多争论以使其发挥作用。问题在于嵌套列表,例如$location其中包含多个值。我想你可以通过调用glue::collapse(sep = ";")每个列表来解决这个问题。有点小技巧,但你最终会得到一个更容易处理的数据结构。试试这个:

library(tidyverse)

extractor <- function(list_element){
  map(list_element, glue::collapse, sep = ";")
}

nested_list %>% 
  map(extractor) %>% 
  transpose() %>% 
  as_tibble() %>% 
  View()

nested_list()是您在问题中包含的数据集的一部分。

于 2017-12-18T16:32:46.327 回答