title: "For" loop with column names as index
body: <p>I would like to create a loop in which the index is given by the column names of a dataframe. The idea is to select one column at a time and create a map based on the data in that column. I need <code>i</code> being the column name, as it identifies the name of the variable and I'll use that as part of the title of the map. However, I do not seem to be able to associate my index <code>i</code> to the name of the column. My code goes as follows:</p>
  <pre><code># random data
  x &lt;- rep(c(&quot;AT130&quot;, &quot;DEA1A&quot;, &quot;DEA2C&quot;, &quot;SE125&quot;, &quot;SE232&quot;), 4)
  y &lt;- c(1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0 ,1, 0, 1, 0, 1)
  z &lt;- c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0, 0, 0, 0, 0)
  w &lt;- c(0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1 ,0, 1, 0, 1, 0)
  d &lt;- as.data.frame(cbind(x,y,z,w))
  colnames(d) &lt;- c(&quot;id&quot;, &quot;typeA&quot;, &quot;typeB&quot;, &quot;typeC&quot;)
  
  
  for (i in colnames(d[,2:ncol(d)])) {
  var_to_map &lt;- d[,c(1,i)]
  
  ## do stuff
  
  }
  </code></pre>
  <p>I get the following error at the first line:</p>
  <pre><code>Error: Can't subset columns that don't exist.
  x Columns `1`, `2`, and `3` don't exist.
  Run `rlang::last_error()` to see where the error occurred.
  </code></pre>
  <p>However, if I just run <code>colnames(d[,2:ncol(d)])</code>, it works properly</p>
  <pre><code>colnames(d[,2:ncol(d)])
   [1] &quot;typeA&quot;             &quot;typeB&quot;       &quot;typeC&quot;   
  </code></pre>
  <p>I could find a workaround by using columns numbers to make it work, but I would like to keep the column names since I am printing (10+) maps within the loop and I am using   <code>i</code> to insert the title of the map each, as follows:</p>
  <pre><code># I use geodata files from the library `Eurostat`.
  geodata &lt;- get_eurostat_geospatial(resolution = &quot;60&quot;, nuts_level = &quot;3&quot;, year = 2013)
   
  
  for (i in colnames(d[,2:ncol(d)])) {
  
  var_to_map &lt;- d[,c(1,i)]
  
  colnames(var_to_map)[1] &lt;- &quot;geo&quot;
  # Joining, by = &quot;geo&quot; 
  map_data &lt;- merge(var_to_map, geodata, by=c(&quot;geo&quot;), all.y=T, all.x=T) 
  ## creating ranges
  
   map_data$cat &lt;- with(map_data, cut(value, 
                                     breaks= qu &lt;- unique(quantile(value, 
                                                                   probs=c(0, 0.2, 0.5, 0.8, 
                                                                           0.9, 0.95, 0.99, 1),
                                                                   na.rm=TRUE,  include.lowest=T )),
                                                          labels=qu[-1]),include.lowest=TRUE )
  
   # Map 
   print(ggplot(data=map_data) + geom_sf(aes(fill=cat),  size=.1) + 
          scale_fill_brewer(palette = &quot;Darkred&quot;, na.value= &quot;grey&quot;) + aes(geometry = geometry) +
          guides(fill = guide_legend(reverse=T, title = &quot;Percentiles&quot;)) +
          labs(title = paste(&quot;The name of this graph is the column name&quot;, i) ## here is where I use the index
               )+ 
          theme_minimal() + theme(legend.position=c(.8,.6)) +
          coord_sf(xlim=c(-12,44), ylim=c(35,70)) +
          theme( axis.text.x=element_blank(), axis.text.y=element_blank())) 
   }
  </code></pre>
  <p>I could also use column numbers for <code>i</code> and create another object with column names to refer to when pasting the title of the map, but I am wondering why the above approach fails and what I could do to make it work in that setting.</p>