title: R divide two dataframes by column
body: <p>I have two large dataframes (with 35,000 x 135 dim each) that also contain a lot NA values. What I want is to divide each column from dataframe A with the contents of the previous column of dataframe B. Specifically, for j = rows and i = columns, A(j,2)/B(j,1), A(j,3)/B(j,2), and so on. So, at the end there would be a new dataframe with dim (35,000 x 134).</p>
  
  <p>I have created this code (due to the large dataset unfortunately the dataframes are not replicable here), but is there a more efficient way to do that?</p>
  
  <p>First dataframe named NNF.data is created after I extract from Fund.data every column that contains the word NNF.</p>
  
  <pre><code>NNF.data &lt;- Fund.data[ , grep("NNF", names(Fund.data))]
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>    NNF.2005.07.EUR NNF.2005.08.EUR NNF.2005.09.EUR
  
  1          NA             NA              NA
  2          NA             NA              NA
  3          NA             NA              NA
  4       -18,066        -83,044          -1,684
  5          NA             NA              NA
  6      1,384,196       1,011,219       3,266,810
  7          NA             NA              NA
  8          NA             NA              NA
  9          NA             NA              NA
  10      99,092         1,478,996       1,118,067
  11    -6,961,028       1,252,678       2,086,868
  12    -2,921,427    -285,339,229     289,599,959
  </code></pre>
  
  <p>Similarly for dataframe named AUM.data</p>
  
  <pre><code>AUM.data &lt;- Fund.data[ , grep("AUM", names(Fund.data))] 
  </code></pre>
  
  <p>Retain the dimensions</p>
  
  <pre><code>columns &lt;- dim(AUM.data)[2] # 135
  rows &lt;- dim(AUM.data)[1] # 35,000
  
  growth_columns &lt;- columns - 1 # Reduce the size of the columns by 1
  Growth.data &lt;- data.frame(matrix(ncol = growth_columns, nrow = rows))
  # Growth.data dim = 35,000 134
  ii &lt;- 1
  for(i in 1:growth_columns){
    ii &lt;- ii+1
    Growth.data[ ,i] &lt;- NNF.data[,ii]/AUM.data[,i]
  }
  </code></pre>
  
  <p>In return I get a warning </p>
  
  <pre><code>In Ops.factor(NNF.data[, k], AUM.data[, i]) : ‘/’ not meaningful for factors
  </code></pre>
  
  <p>Is there a more efficient way to do it?</p>