title: how to fix Error " Error in `[.data.frame`(data, , all.vars(Terms), drop = FALSE) : undefined columns selected" in caret package
body: <p>i encounter this error :</p>
  <blockquote>
  <p>Error in <code>[.data.frame</code>(data, , all.vars(Terms), drop = FALSE) :
  undefined columns selected</p>
  </blockquote>
  <p>when i use <code>caret</code> to do regression with using bootstrap for 3 different cluster(index column)</p>
  <pre><code>library(&quot;tidyverse&quot;)
  library(&quot;lattice&quot;)
  
  library(&quot;caret&quot;)
  library(&quot;janitor&quot;)
  data&lt;- read.csv(&quot;C:/Users/asus/Desktop/test.csv&quot;,header = TRUE)
  mydata &lt;- data.frame(index = data$cluster,
                       x     = data[,3:4],
                       y     = data[,5])
  tab &lt;- table(mydata$index)
  tab
  sample_n(mydata, 3)
  attach(mydata)
  mylist &lt;- list()
  mydata &lt;- clean_names(mydata)
  head(mydata)
  for (i in 1:length(unique(mydata$index))) {
    # define training control
    train.ctrl &lt;- trainControl(method = &quot;boot&quot;, number = tab[i])
    # train the model
    mylist[[i]] &lt;- train(mydata[index == i,&quot;y&quot;] ~ mydata[index ==i,&quot;x_xa&quot;] + mydata[index == i,&quot;x_xb&quot;], data = data.frame(mydata), method = &quot;lm&quot;,
                         trControl = train.ctrl)
    print(mylist[[i]])
    summary(mylist[[i]])
  }
  </code></pre>
  <p>here you can see my data:</p>
  <p><img src="https://i.stack.imgur.com/uwouP.jpg" alt="enter image description here" /></p>