title: Error with loops in R: Error "object not found"
body: <p>I have panel data with years from 2005 to 2015 and sectors from 1 to 33 (excluding 2, 4 and 31). I would like to run some loops and save the output for each year-sector combination separately. This is my code:</p>
  <pre><code>    for (i in 2005:2015){
  
    ntm_data &lt;-subset(ntm_data_wip, StartDate &lt;=i &amp; EndDate &gt;i)
    
  for(s in c(1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32,  33)){
    
    ntm_data &lt;-subset(ntm_data, ISIC4==s)
    
  # Once the data is loaded, I exclude NTM codes which are missing. 
  # I only need the reporter, NTM code and product codes (HS 6-digit codes).
  ntm_data &lt;- ntm_data[!is.na(ntm_data$ntmcode)&amp;ntm_data$ntmcode!=&quot;&quot;,]
  ntm_data &lt;- ntm_data[,c(&quot;reporter&quot;, &quot;ntmcode&quot;, &quot;hs6&quot;)]
  
  # I group the data by reporter, NTM and product code (hs6) and count the number of combinations in a new variable called count.
  ntm_data &lt;- ntm_data %&gt;% group_by(reporter, ntmcode, hs6) %&gt;%
    summarise(count = n())
  head(ntm_data)
  
  # I prepare the regulatory matrix by creating a list of countries for which I want the regulatory distance.  The 
  # regulatory matrix shows the distance between two countries and has as column and row names the ISO3 codes of the countries.
  # As specified above, I am interested in having the analysis for all available countries. 
  avail_iso3s &lt;- unique(ntm_data$reporter)
  
  # I create an empty regulatory distance matrix. For column size I use the length of avail_iso3s and add 1 for the reporter column.
  # I populate the column names with reporter and the ISO3 codes with the option dimnames.
  regulatory_distance_matrix &lt;- data.frame(matrix(vector(),0,length(avail_iso3s)+1,
                                                  dimnames = list(c(), c(&quot;reporter&quot;, avail_iso3s )
                                                  )),
                                           stringsAsFactors=F)
  
  
  #' Now I can move on to calculating the regulatory distance formula in page 3 of &quot;DEEP REGIONAL INTEGRATION AND NON-TARIFF MEASURES:A METHODOLOGY FOR DATA ANALYSIS (2015)&quot; . 
  #' As N is a constant, I start with calculating it outside of the loop
  N &lt;- ntm_data %&gt;% group_by(ntmcode, hs6) %&gt;% count()
  N &lt;- nrow(N)
  
  # I now fill in the regulatory distance matrix with values
  
  for (g in 1:length(avail_iso3s)){
    country_a &lt;- ntm_data[ntm_data$reporter==avail_iso3s[g],c(&quot;ntmcode&quot;, &quot;hs6&quot;)]
    country_a$country_a &lt;- 1
    regulatory_distance_matrix[g,&quot;reporter&quot;] &lt;- avail_iso3s[g]
    
    for (k in 1:length(avail_iso3s)){
      
      if (!is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){next }
      
      country_b &lt;- ntm_data[ntm_data$reporter==avail_iso3s[k],c(&quot;ntmcode&quot;, &quot;hs6&quot;)]
      country_b$country_b &lt;- 1
      merged &lt;- merge(country_a, country_b, by=c(&quot;ntmcode&quot;, &quot;hs6&quot;), all = TRUE)
      merged[is.na(merged)] &lt;- 0
      merged$abs_diff &lt;- abs(merged$country_a-merged$country_b)
      rd &lt;- sum(merged$abs_diff)/N
      regulatory_distance_matrix[g,avail_iso3s[k]] &lt;- rd
      
    }
  }
  
  # Now I fill in the missing values and create a Stata dta.file.
  for (g in 1:length(avail_iso3s)){
    for (k in 1:length(avail_iso3s)){
      if (is.na(regulatory_distance_matrix[k,avail_iso3s[g]])){
        regulatory_distance_matrix[k,avail_iso3s[g]] &lt;- regulatory_distance_matrix[g,avail_iso3s[k]]
      }
    }
  }
  
  
  regulatory_distance_matrix$year &lt;-i
  regulatory_distance_matrix$ISIC4 &lt;-s
  
  write.dta(regulatory_distance_matrix, paste0(&quot;C:/Users/Utente/Desktop/Master's thesis/Thesis analysis/- RD construction/Binary sectoral RD/regulatory_distance_matrix_&quot;,i,&quot;_&quot;,s,&quot;.dta&quot;))
  } 
  }
  </code></pre>
  <p>However, after the first file (regulatory_distance_matrix_&quot;,i,&quot;_&quot;,s,&quot;.dta&quot;) is correctly created, I get the following error during the creation of the second file:</p>
  <pre><code> Error in eval(e, x, parent.frame()) : oggetto &quot;ISIC4&quot; non trovato
  </code></pre>
  <p>Does someone how to fix this issue?
  Thanks in advance!</p>
  <p>EDIT:</p>
  <pre><code>&gt; dput(head(ntm_data_wip))
  structure(list(reporter = c(&quot;TUR&quot;, &quot;ARG&quot;, &quot;BRA&quot;, &quot;CHN&quot;, &quot;USA&quot;, 
  &quot;EUN&quot;), Reporter_ISO_N = c(&quot;792&quot;, &quot;032&quot;, &quot;076&quot;, &quot;156&quot;, &quot;842&quot;, 
  &quot;918&quot;), hs6 = c(&quot;910610&quot;, &quot;851679&quot;, &quot;040221&quot;, &quot;620449&quot;, &quot;021012&quot;, 
  &quot;284990&quot;), ntmcode = c(&quot;B31&quot;, &quot;A11&quot;, &quot;B33&quot;, &quot;B83&quot;, &quot;A83&quot;, &quot;B33&quot;
  ), partner = c(&quot;TON&quot;, &quot;WLD&quot;, &quot;WLD&quot;, &quot;IRN&quot;, &quot;VAT&quot;, &quot;WLD&quot;), Partner_ISO_N = c(&quot;776&quot;, 
  &quot;000&quot;, &quot;000&quot;, &quot;364&quot;, &quot;336&quot;, &quot;000&quot;), nbr = c(1L, 1L, 1L, 1L, 2L, 
  1L), Year = c(2016L, 2014L, 2013L, 2016L, 2017L, 2011L), NTMNomenclature = c(&quot;M4&quot;, 
  &quot;M4&quot;, &quot;M4&quot;, &quot;M4&quot;, &quot;M4&quot;, &quot;M4&quot;), NomenCode = c(&quot;H4&quot;, &quot;H4&quot;, &quot;H4&quot;, 
  &quot;H4&quot;, &quot;H4&quot;, &quot;H3&quot;), Dataset_id = c(161L, 174L, 174L, 131L, 179L, 
  111L), ntm_1_digit = c(&quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;), StartDate = c(2015L, 
  2006L, 2008L, 2011L, 1992L, 2009L), EndDate = c(9999L, 9999L, 
  9999L, 9999L, 9999L, 2011L), new_ISIC4 = c(&quot;32&quot;, &quot;28&quot;, &quot;10&quot;, 
  &quot;13&quot;, &quot;10&quot;, &quot;19&quot;), ISIC4 = c(32L, 28L, 10L, 13L, 10L, 19L)), datalabel = &quot;&quot;, time.stamp = &quot;31 Jul 2021 11:34&quot;, formats = c(&quot;%9s&quot;, 
  &quot;%9s&quot;, &quot;%9s&quot;, &quot;%9s&quot;, &quot;%9s&quot;, &quot;%9s&quot;, &quot;%9.0g&quot;, &quot;%12.0g&quot;, &quot;%9s&quot;, 
  &quot;%9s&quot;, &quot;%12.0g&quot;, &quot;%9s&quot;, &quot;%10.0g&quot;, &quot;%10.0g&quot;, &quot;%9s&quot;, &quot;%10.0g&quot;), types = c(3L, 
  3L, 6L, 4L, 3L, 3L, 65530L, 65529L, 2L, 3L, 65529L, 1L, 65529L, 
  65529L, 2L, 65530L), val.labels = structure(c(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;), .Names = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;)), var.labels = c(&quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;Number of NTM, distinct codes&quot;, &quot;&quot;, &quot;&quot;, 
  &quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;(min) StartDate&quot;, &quot;(max) EndDate&quot;, &quot;&quot;, &quot;&quot;), version = 118L, label.table = list(), expansion.fields = list(
      c(&quot;ISIC4&quot;, &quot;destring&quot;, &quot;Characters removed were:&quot;), c(&quot;ISIC4&quot;, 
      &quot;destring_cmd&quot;, &quot;destring new_ISIC4, gen(ISIC4)&quot;)), byteorder = &quot;LSF&quot;, orig.dim = c(6953474L, 
  16L), row.names = c(NA, 6L), class = &quot;data.frame&quot;)
  </code></pre>