title: Element-wise operation with two vectors of a data frame in R
body: <p>My first question here: how to apply an efficient routine that iterates values of two vectors (pairwise) of a given data frame?</p>
  
  <p>To be more specific, consider the following example using the following data frame:</p>
  
  <pre><code>df0 &lt;- data.frame(matrix(c(1,2,2,3,1,3,0.4,0.2,0.2,0.1,0.4,0.1),nrow=6,ncol=2))
  colnames(df0) &lt;- c("value","frequency")
  </code></pre>
  
  <p>The first column is a real value and the second column is a frequency (or weights). NOTICE: the weights have to be strictly positive, they might be repeated, they not necessarily add up to one (because of repetition).</p>
  
  <p>I am performing the following LOOP to calculate my function P. This P is supposed to be a number between 0 and 1.</p>
  
  <pre><code># Define two parameters
  K = 1/2
  alpha = 0
  
  # LOOP
  mattemp &lt;- matrix(,nrow=length(df0$value), ncol=length(df0$value))
  
  for(i in 1:length(df0$value)) {
    for(j in 1:length(df0$value)) {
  
      mattemp[i,j] &lt;- df0$frequency[i]^(1+alpha) * df0$frequency[j] * abs(df0$value[i]-df0$value[j])
  
      P &lt;- K * sum(mattemp)
    }
  }
  </code></pre>
  
  <p>Basically, my function P is calculating: </p>
  
  <pre><code>P = K * (0.4^alpha * 0.2 * |1-2| + 0.4^alpha * 0.1 * |1-3| + ...
  </code></pre>
  
  <p>This code works perfectly well as long as the matrix is small. </p>
  
  <p>However, I am trying to implement this routine for a big matrix (5400 x 5400) and this LOOP does not seem to find an end.</p>
  
  <p>I already tried to loop it using a <code>foreach</code> command (using <code>%dopar%</code>), but it does not work as well.</p>
  
  <p>Is there a smart and concise routine that R can handle??? It does not need to follow the above structure, as long as it is efficient.</p>
  
  <p>Thank you very much</p>