Computation of concordant and discordant pairs I would like to compute the number of concordant and discordant pairs of two large vectors. This function is a nice try, and works quite efficiently.

pairs &lt;- function(x,y){
    n &lt;- length(x)
    ix &lt;- order(x)
    x &lt;- x[ix]
    y &lt;- y[ix]
    Nc &lt;- sum(sapply(1:(n-1),function(i) sum(x[i]&lt;x[(i+1):n] &amp; y[i]&lt;y[(i+1):n])))
    Nd &lt;- sum(sapply(1:(n-1),function(i) sum(x[i]&lt;x[(i+1):n] &amp; y[i]&gt;y[(i+1):n])))
    return(list(Nc=Nc,Nd=Nd))
}

x &lt;- runif(10000)
y &lt;- runif(10000)
system.time(pairs(x,y))


Do you have any idea on how it would be possible to boost the function?
These sorts of operations are tough to vectorize because you need to compare every element to all later elements. If the counting operation is a performance bottleneck in your code, then one possibility would be to implement it in C++ using the Rcpp package:

library(Rcpp)
cppFunction(
  "IntegerVector CDcount(NumericVector x, NumericVector y) {
    IntegerVector counts(2, 0);
    int n = x.size();
    for (int i=0; i &lt; n; ++i) {
      for (int j=i+1; j &lt; n; ++j) {
        counts[0] += (x[i] &lt; x[j]) &amp;&amp; (y[i] &lt; y[j]);
        counts[1] += (x[i] &lt; x[j]) &amp;&amp; (y[i] &gt; y[j]);
      }
    }
    return counts;
  }")

pairs2 &lt;- function(x, y) {
  n &lt;- length(x)
  ix &lt;- order(x)
  x &lt;- x[ix]
  y &lt;- y[ix]
  counts &lt;- CDcount(x, y)
  return(list(Nc=counts[1], Nd=counts[2]))
}


For your test data, this results in a 50 times speedup on my system.

x &lt;- runif(10000)
y &lt;- runif(10000)
system.time(pairs(x,y))
#    user  system elapsed 
#   4.892   1.447   6.584 
system.time(pairs2(x, y))
#    user  system elapsed 
#   0.119   0.001   0.120 
identical(pairs(x, y), pairs2(x, y))
# [1] TRUE


The Rcpp package takes some time to compile the CDcount function, so this option is probably only helpful if you are operating on very large vectors or if you are repeating the operation many times.