title: Creating a loop in R which also changes the column name
body: <p>I am attempting to loop a command based upon a list (fish_species). And while I’ve found plenty of examples, I haven’t found one that also includes changing the column name as part of the loop.  I have figured out how to get the desired result for an individual species (lines 10-13), but in the actual dataset I have ~500 species, and I’d prefer not to repeat this command 500+ times. Is there a way to substitute the values from a list where it says variable?</p>
  
  <pre><code>Fishdata$variable &lt;- ifelse(fishdata$Species== “variable”,fishdata$Number,0)
  </code></pre>
  
  <p>I know how to do this is ArcGIS, but I am trying to expand my horizons and learn R. This is also my first post, so please excuse any screw ups. 
  Thank you for any help you can provide. </p>
  
  <pre><code>fishdata &lt;-c()
  fishdata$Site &lt;-c(1,1,1,2,2,2)
  fishdata$Species &lt;- c("one_fish", "two_fish", "two_fish", "red_fish", "blue_fish", "blue_fish")
  fishdata$Number &lt;- c(1,1,1,1,1,1)
  fishdata$one_fish &lt;-0
  fishdata$two_fish &lt;-0
  fishdata$red_fish &lt;-0
  fishdata$blue_fish &lt;-0
  fish_list &lt;- c("one_fish","two_fish", "red_fish", "blue_fish")
  fishdata$one_fish &lt;- ifelse(fishdata$Species=="one_fish",fishdata$Number,0)
  fishdata$two_fish &lt;- ifelse(fishdata$Species=="two_fish",fishdata$Number,0)
  fishdata$red_fish &lt;- ifelse(fishdata$Species=="red_fish",fishdata$Number,0)
  fishdata$blue_fish &lt;- ifelse(fishdata$Species=="blue_fish",fishdata$Number,0)
  </code></pre>