title: RVest: How to fix 'Replacement has length zero' in a loop
body: <p>i would like to scrape a specific bit of html-code out of a website. The desired object isn't on every page of the loop below, therefore, some values are NA. But whenever an object is missing, the whole loop stops.</p>
  
  <p>I tried this code for non-loop scraping, which turned out to work perfectly.
  Here i excluded all values that are either unobtainable or not of interest.</p>
  
  <p><code>BJ &lt;- data.frame(
    Wohnungs_ID = ID,
    Baujahr = ifelse (nchar(Baujahr)==0 | nchar(Baujahr)&gt;6, NA, Baujahr),
    stringsAsFactors = F</code></p>
  
  <p>In the relevant case, i want to scrape two specific variables, that are not always listed. Url is the list of reflinks i scraped ealier.</p>
  
  <pre><code>library(rvest)  
  library(magrittr)
  library(tidyr)  
  library(ggplot2) 
  library(stringr) 
  library(jsonlite) 
  library(purrr)  
  library(dplyr)  
  
  immo_webp &lt;- read_html ("https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz")
  
  
  # URLs 
  
  base = 'https://www.immobilienscout24.de/expose/'
  
  urls &lt;- sapply(read_html("https://www.immobilienscout24.de/Suche/S-2/Wohnung-Miete/Rheinland-Pfalz/Koblenz")%&gt;%
                   html_nodes('article')%&gt;%
                   html_attr('data-obid'), function (url){paste0(base, url)})
  print(urls)
  
  # ID bilden
  
  substring(urls, 1, 1)
  ID &lt;- substring(urls, 41)
  
  Baujahr &lt;- c(1 : 20)
  Energie &lt;- c(1 : 20)
  
  for (i in urls){
  
    aktuellewohnung &lt;- read_html(url(i))
  
    Baujahr[i] &lt;- aktuellewohnung %&gt;%
      html_nodes("#is24-content &gt; div.grid-item.padding-desk-right-xl.desk-two-thirds.lap-one-whole.desk-column-left.flex-item.palm--flex__order--1.lap--flex__order--1 &gt; div.is24-ex-details.main-criteria-headline-size.two-column-layout &gt; div.criteriagroup.flex.flex--wrap.criteria-group--spacing.padding-top-s &gt; div.criteriagroup.criteria-group--border.criteria-group--two-columns.criteria-group--spacing &gt; dl:nth-child(1) &gt; dd") %&gt;%
      html_text()
    Energie[i] &lt;- aktuellewohnung %&gt;%
      html_nodes("#is24-content &gt; div.grid-item.padding-desk-right-xl.desk-two-thirds.lap-one-whole.desk-column-left.flex-item.palm--flex__order--1.lap--flex__order--1 &gt; div.is24-ex-details.main-criteria-headline-size.two-column-layout &gt; div.criteriagroup.flex.flex--wrap.criteria-group--spacing.padding-top-s &gt; div.criteriagroup.criteria-group--border.criteria-group--two-columns.criteria-group--spacing &gt; dl:nth-child(7) &gt; dd") %&gt;%
      html_text()  
  } 
  </code></pre>
  
  <p>I get the following error message:</p>
  
  <pre><code>Error in Baujahr[i] &lt;- aktuellewohnung %&gt;% html_nodes("#is24-content &gt; div.grid-item.padding-desk-right-xl.desk-two-thirds.lap-one-whole.desk-column-left.flex-item.palm--flex__order--1.lap--flex__order--1 &gt; div.is24-ex-details.main-criteria-headline-size.two-column-layout &gt; div.criteriagroup.flex.flex--wrap.criteria-group--spacing.padding-top-s &gt; div.criteriagroup.criteria-group--border.criteria-group--two-columns.criteria-group--spacing &gt; dl:nth-child(1) &gt; dd") %&gt;%  : 
  
  replacement has length zero
  </code></pre>
  
  <p>I want this loop to continue and save the missing value as NA.
  Hopefully, you can help me. 
  Thanks a lot in advance.</p>