title: apply a function from different list in R using sweep function
body: <p>I have two list and each list have multiple elements, and each element is a data frame. As an example</p>
  
  <pre><code>set.seed(1234)
  k = data.frame(a=rnorm(5,10),b=rnorm(5,10))
  l = data.frame(a=rnorm(5,3),b=rnorm(5,3))
  
  lista &lt;- list(k=k,l=l)
  
  k = data.frame(x=rnorm(5,20))
  l = data.frame(x=rnorm(5,6))
  
  listb &lt;- list(k=k,l=l)
  
  
  
  &gt; lista
  $k
            a         b
  1  8.792934 10.506056
  2 10.277429  9.425260
  3 11.084441  9.453368
  4  7.654302  9.435548
  5 10.429125  9.109962
  
  $l
           a        b
  1 2.522807 2.889715
  2 2.001614 2.488990
  3 2.223746 2.088805
  4 3.064459 2.162828
  5 3.959494 5.415835
  
  
  &gt; listb
  $k
           x
  1 20.13409
  2 19.50931
  3 19.55945
  4 20.45959
  5 19.30628
  
  $l
           x
  1 4.551795
  2 6.574756
  3 4.976344
  4 5.984862
  5 5.064051
  </code></pre>
  
  <p>I would like to take each column from lista and a <strong>corresponding column</strong> in listb and apply the following formula:</p>
  
  <pre><code>(lista$k[1]-listb$k[1])/(abs(lista$k[1])+abs(listb$k[1]))
  (lista$k[2]-listb$k[1])/(abs(lista$k[2])+abs(listb$k[1]))
  </code></pre>
  
  <p>repeat the same for <code>"l"</code>.</p>
  
  <p>I tried using sweep function and Map in R as follows:</p>
  
  <pre><code>form &lt;- Map(function(x,y) abs(sweep(x,1,y,FUN="-"))/(sweep(abs(x),1,abs(y),FUN="+")),lista,listb)
  </code></pre>
  
  <p>I get the following error, any help would be greatly appreciated</p>
  
  <pre><code>Error in Ops.data.frame(x, aperm(array(STATS, dims[perm]), order(perm)),  : 
    list of length 10 not meaningful
  </code></pre>
  
  <p><strong>EDIT</strong>: Converting data.frame into matrix is able to provide desired output.</p>
  
  <pre><code>form &lt;- Map(function(x,y) abs(sweep(as.matrix(x),1,as.matrix(y),FUN="-"))/(sweep(abs(as.matrix(x)),1,abs(a‌​s.matrix(y)),FUN="+")),lista,listb)
  </code></pre>