title: Why does column not read as a factor when it is accessed with a counter dummy variable?
body: <p>I have created a loop which I am using to select all of the factor variables from a large data frame. However the loop does not work because the use of a counter to index the column for some reason doesn't read as a factor. Why is this? And how can I get this loop to work:</p>
  
  <pre><code>#Here's the loop:
  y &lt;- data.frame(c = 1:2661)
  for (i in 1:ncol(x)){
    ifelse(is.factor(x[i]) == FALSE, y &lt;- cbind(y, x[i]), y &lt;- y)
  }
  </code></pre>
  
  <p>And the problem is clearly in the use of "i" to reference the column. For example:</p>
  
  <pre><code>#sample data 
  df &lt;- as.data.frame(structure(c(2L, 2L, 2L, 2L, 2L), 
                            .Label = c("female", "male"), class = "factor"))
  
  names(df)[names(df) == "structure(c(2L, 2L, 2L, 2L, 2L), .Label = c(\"female\", \"male\"), class = \"factor\")"] &lt;- "var"
  
  #reference the column name directly
  is.factor(df$var)
  [1] TRUE
  #use a counter to access the variable:
  i &lt;- 1
  is.factor(df[i])
  [1] FALSE
  </code></pre>
  
  <p>Is this something to do with R or is there something up with my PC? 
  If it is something to do with R, can anyone explain what is going on and how to get my loop to function?</p>