title: How do you scrape items together so you don't lose the index?
body: <p>I am doing some basic webscraping with RVest and am getting results to return, however the data isnt lining up with each other.  Meaning, I am getting the items but they are out of order from the site so the 2 data elements I am scraping cant be joined in a data.frame.</p>
  
  <pre><code>library(rvest)
  library(tidyverse)
  
  base_url&lt;- "https://www.uchealth.com/providers"
  loc &lt;- read_html(base_url) %&gt;%
    html_nodes('[class=locations]') %&gt;%
    html_text() 
  dept &lt;- read_html(base_url) %&gt;%
    html_nodes('[class=department last]') %&gt;%
    html_text()
  </code></pre>
  
  <p>I was expecting to be able to create a dataframe of :</p>
  
  <pre><code>Location  Department
  </code></pre>
  
  <p>Any suggestions?  I was wondering if there is an index that would keep these items together but I didnt see anything.</p>
  
  <p>EDIT:  I tried this also and did not have any luck.  It seems the location is getting an erroneous starting value:</p>
  
  <pre><code>scraping &lt;- function(
  
  base_url = "https://www.uchealth.com/providers"
  )
  {
  loc &lt;- read_html(base_url) %&gt;%
    html_nodes('[class=locations]') %&gt;%
    html_text() 
  
  dept &lt;- read_html(base_url) %&gt;%
    html_nodes('[class=specialties]') %&gt;%
    html_text()
  
  data.frame(
    loc = ifelse(length(loc)==0, NA, loc),
    dept = ifelse(length(dept)==0, NA, loc), 
    stringsAsFactors=F
  )
  
  }
  </code></pre>