title: Error in .subset2(x, i, exact = exact) : subscript out of bounds in R
body: <p>I am trying to loop over specific numeric columns from dataframe, the goal is to
  extract correlations and p-values using &quot;cor.test&quot; function.</p>
  <p>The correlation consists in calculate the linear relationship of
  one categorical variable composed of 0 and 1 values against each specific numeric column.</p>
  <p>Here's my code so far:</p>
  <pre><code>## data ##
  names &lt;- c(&quot;John&quot;, &quot;Greg&quot;, &quot;Maria&quot;, &quot;Josh&quot;, &quot;Emma&quot;)
  categorical_column &lt;- sample(0:1, 5, replace = TRUE)
  numeric_column_1  &lt;- sample(1:30, 5, replace = TRUE)
  numeric_column_2 &lt;- sample(1:40, 5, replace = TRUE)
  
  sampled_df &lt;- data.frame(names, categorical_column, numeric_column_1,
                           numeric_column_2)
  
  
  ## specific columns ##
  
  numerical_columns &lt;- c(&quot;numeric_column_1&quot;, &quot;numeric_column_2&quot;)
  
  
  ## for-loop task ##
  
  for(i in seq_along(numerical_columns)){
        
         correlation_num_df &lt;- structure(list(
                           
         variable &lt;- numerical_columns,
                          
         correlation &lt;- cor.test(sampled_df[numerical_columns[i]][[i]],
                         sampled_df[[&quot;categorical_column&quot;]])[[&quot;estimate&quot;]][[&quot;cor&quot;]],
                          
         p_value &lt;- cor.test(sampled_df[numerical_columns[i]][[i]],
                        sampled_df[[&quot;categorical_column&quot;]])[[&quot;p.value&quot;]]
        ),
      
       class = &quot;data.frame&quot;,
       
       nrow = c(NA, -2L))
        
     
  }
  
  
  </code></pre>
  <p>Console output:</p>
  <pre><code>Error in .subset2(x, i, exact = exact) : subscript out of bounds
  </code></pre>
  <p>How could I know the subset that is out of bounds? And how could I fix it?</p>