title: R - How can I use the apply functions instead of iterating?
body: <h2>Regress each dependent variable ( dep_var ) against independent variable ( ind_var )</h2>
  
  <p>I am trying to perform linear regressions for multiple dependent variables against a independent variable <strong><em>one at a time.</em></strong>  </p>
  
  <p>When there is a missing observation (<em>NA</em>) , the entire row is not used for that particular regression.</p>
  
  <p>I have done it by looping/iterating through each column of dependent variable. </p>
  
  <pre><code>fit = list()
  for( i in 1 : 2 ) {
      fit[[i]] = lm( mydf$Ind_Var[ which( !is.na( mydf[  , (2+i) ] ) ) ] ~ na.omit( mydf[ , (2+i) ] ) )
      }
  </code></pre>
  
  <p>Without having to involve other packages ( let's restrict to functions like <em>lm</em>, <em>apply family functions</em> , <em>do/do.call</em>), how can I do so? </p>
  
  <h3>Random Data</h3>
  
  <pre><code>mydf = data.frame( 
  "ID"    = rep( "A" , 25 ),
  "Date"  = c( 1 : 25 ), 
  "Dep_1" = c( 0.78670185, 0.15221561, NA, 0.85270392, 0.90057399, 0.75974473, 0.42026760, 0.64035871, 0.83012434, 0.04985492, 0.06619375, 0.36024745, 0.83969627, 0.45293842, 0.25272036, NA, 0.63783321, 0.42294695, 0.06726004, 0.14124547, 0.54590193, 0.99560087, 0.14255501, 0.41559977, 0.80120970) ,          
  "Dep_2" = c( 0.736137983, 0.979317444, 0.901380500, 0.942325049, 0.420741297, NA, 0.243408607, 0.824064331, 0.462912557, NA, 0.710834065, 0.264922818, 0.797917063, 0.578866651, 0.955944058, 0.291149075, 0.437322581, 0.298153168, 0.579299049, 0.671718144, 0.545720702, 0.099175216, 0.808933227, 0.912825535, 0.417438973 ) ,          
  "Ind_Var" = c( 75:51 )  )
  </code></pre>
  
  <hr>
  
  <p>My own attempt of converting will be:</p>
  
  <pre><code>apply( mydf[ ,-c(1:2) ] , 2 , function( x ) lm( mydf$Ind_Var[ which( !is.na( x ) ) ] ~ na.omit(x)  ) )
  </code></pre>
  
  <p>but this involves having <em>mydf</em> hardcoded.</p>
  
  <p>I apologize if I have used any incorrect terms.</p>