title: I am trying to change the working directory within a loop based on the level of a factor variable in R
body: <p>I work in a school district and plan to produce unique PDF reports that I will send to a shared drive containing a folder for each of the 40+ schools in my district.  </p>
  
  <p>I am trying to set the working directory within my <code>for</code> loop so that each school will get a report tailored for their school.  The schools files are named based on the levels of some variable named "school_name" and now I want to write to those folders based upon the fact that I am using data for school[i].</p>
  
  <pre><code>schools &lt;- levels(as.factor(data$school_name))
  for (i in 1:length(schools)) {
    setwd("W:\\Data Analysis Results\\"paste(schools[[i]]))
    ## some command to create a unique PDF report for school [[i]]
  }
  </code></pre>