1

我正在做一个网络抓取/映射项目,我从餐厅网站抓取地址数据,并将结果存储为一个列表 - 在这个例子中,称为loc_list.

问题是,如何最好地将这些列表项转换为单个 data.frame / tibble (当前使用bind_rows( )),但在新的 data.frame 中,还有一个标题列metro对应于每个列表项名称。在我的示例中,输出将有 3alpharetta秒,然后是 3 atlanta,然后是 1 buford

loc_list

$alpharetta
# A tibble: 3 x 2
                  names                                                  address
                  <chr>                                                    <chr>
1 East Roswell          US 2630 Holcomb Bridge Rd Alpharetta, GA  30022
2 Old Milton US 4305 Old Milton Parkway Ste 101 Alpharetta, GA  30022
3 Windward       US 875 N Main Street Ste 306 Alpharetta, GA  30009

$atlanta
# A tibble: 3 x 2
                        names                                         address
                        <chr>                                           <chr>
1 Philips Arena        US 100 Techwood Drive Atlanta, GA  30303
2 Virginia Highlands       US 1006 N Highland Ave Atlanta, GA  30306
3 Perimeter     US 1211 Ashford Crossing Atlanta, GA  30346

$buford
# A tibble: 1 x 2
            names                                          address
            <chr>                                            <chr>
1 Woodward US 3250 Woodward Crossing Blvd Buford, GA  30519

目标输出:

names          address       metro
East Ros...    US 2630...    alpharetta
4

1 回答 1

2

正如 alistaire 指出bind_rows的那样,.id. 这是示例数据:

alpharetta <- tibble(names=c("East Roswell", "Old Milton"),
                     address = c("US 2630 Holcomb Bridge Rd Alpharetta, GA  30022", "4305 Old Milton Parkway Ste 101 Alpharetta, GA  30022"))
atlanta <- tibble(names=c("Philips Arena", "Virginia Highlands"),
                  address = c("US 100 Techwood Drive Atlanta, GA  30303", "US 1006 N Highland Ave Atlanta, GA  30306"))

loc_list <- list(alpharetta = alpharetta, atlanta = atlanta)

bind_rows(loc_list, .id="metro")
# A tibble: 4 x 3
       metro              names                                               address
       <chr>              <chr>                                                 <chr>
1 alpharetta       East Roswell       US 2630 Holcomb Bridge Rd Alpharetta, GA  30022
2 alpharetta         Old Milton 4305 Old Milton Parkway Ste 101 Alpharetta, GA  30022
3    atlanta      Philips Arena              US 100 Techwood Drive Atlanta, GA  30303
4    atlanta Virginia Highlands             US 1006 N Highland Ave Atlanta, GA  30306
于 2017-11-21T07:23:01.550 回答