title: Looping over multiple variables in R
body: <p>I have been using Stata and the loops are easily executed there. However, in R I have faced some errors in looping over variables. I tried some of the codes over here and it does not work. Basically, I am trying to clean the data by logging the values. I had to convert negative values to positive first before logging them.</p>
  
  <p>I intend to loop over multiple firm statistics on the dataframe but I faced errors in doing so.</p>
  
  <pre><code>varlist &lt;- c("revenue", "profit", "cost")`
  
  for (v in varlist) {
    data$log_v &lt;- log(abs(ifelse(data$v&gt;1, data$v, NA)))
    data$log_v &lt;- ifelse(data$v&lt;0, data$log_v*-1,data$log_v)
  }
  </code></pre>
  
  <blockquote>
    <p>Error in $&lt;-.data.frame(<em>tmp</em>,"log_v", value = numeric(0)) : replacement has 0 rows, data has 9</p>
  </blockquote>