title: R loop over string and use it to refer to column names
body: <p>I have data frame with column names 1990.x ..2000.x, 1990.y,..2000.y. I want to replace NAs in variables ending with ".x" with values from .y from corresponding year. It is element by element computation of formula 1990.x = 0.5+0.2*log(1990.y)</p>
  
  <p>I wanted to do something like this:</p>
  
  <pre><code>for (v in colnames(df[ ,grepl(".x",names(df))])) {
    print(v)
    df$v &lt;- ifelse(is.na(df$v), ols$coefficients[1]+ols$coefficients[2]*log(df$gsub(".x",".y",v)), df$v)
  }
  </code></pre>
  
  <p>but this is not working. How can i make this loop working, or is there any better solution?
  Thanks</p>