将列表转换为Tibble Plus添加具有列表名称的列

时间:2017-11-21 01:18:06

标签: r list tidyverse tibble

我正在进行网络抓取/制图项目,我从餐馆网站上删除了地址数据,并将结果存储为列表 - 在此示例中称为{{1 }}。

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

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

1 个答案:

答案 0 :(得分:1)

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