2

I'm working on a web scraping / mapping project where I've scraped address data from a restaurant website and I've stored the results as a list - in this example, called loc_list.

Question is, how best to convert these list items into a single data.frame / tibble (currently using bind_rows( )) but ALSO, in the new data.frame, have a column titled metro which corresponds to each list item name. In my example, the output would have 3 alpharettas, followed by 3 atlanta, then 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

Targeted output:

names          address       metro
East Ros...    US 2630...    alpharetta
2
  • 3
    bind_rows(loc_list, .id = "metro") You can also use purrr::map_df with a .id parameter, if you're applying a function to get to this state.
    – alistaire
    Nov 21, 2017 at 1:25
  • FWIW you're actually told to use dput() to share data in questions in the actual R tag top text. #nojoke
    – hrbrmstr
    Nov 21, 2017 at 2:57

1 Answer 1

2

As alistaire pointed out bind_rows is enough with .id. Here is example data:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.