title: turn some variables in `data.frame` into `factor` keeping the data.frame structure R
body: <p>I'm trying to turn some of the variables in my <code>data.frame</code> into <code>factor</code> while preserving the data.frame structure. I follow the suggestions <a href="https://stackoverflow.com/a/9252447/8840683">HERE</a> but I don't get my desired output, any fix?</p>
  
  <pre><code>d &lt;- data.frame(a = c(1,2, 3, 5), b = c(2,3, 4, 2), e = c(3,4,5,1), f = rep(c("long", "short"), 2))
  factor.name &lt;- names(d)[-4]
  
  d[] &lt;- lapply(seq_along(factor.name), function(i) as.factor(d[factor.name[i]]))
  </code></pre>