Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a R function that applies a function to each pair of columns?
    text
    copied!<p>I often need to apply a function to each pair of columns in a dataframe/matrix and return the results in a matrix. Now I always write a loop to do this. For instance, to make a matrix containing the p-values of correlations I write:</p> <pre><code>df &lt;- data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100)) n &lt;- ncol(df) foo &lt;- matrix(0,n,n) for ( i in 1:n) { for (j in i:n) { foo[i,j] &lt;- cor.test(df[,i],df[,j])$p.value } } foo[lower.tri(foo)] &lt;- t(foo)[lower.tri(foo)] foo [,1] [,2] [,3] [1,] 0.0000000 0.7215071 0.5651266 [2,] 0.7215071 0.0000000 0.9019746 [3,] 0.5651266 0.9019746 0.0000000 </code></pre> <p>which works, but is quite slow for very large matrices. I can write a function for this in R (not bothering with cutting time in half by assuming a symmetrical outcome as above):</p> <pre><code>Papply &lt;- function(x,fun) { n &lt;- ncol(x) foo &lt;- matrix(0,n,n) for ( i in 1:n) { for (j in 1:n) { foo[i,j] &lt;- fun(x[,i],x[,j]) } } return(foo) } </code></pre> <p>Or a function with Rcpp:</p> <pre><code>library("Rcpp") library("inline") src &lt;- ' NumericMatrix x(xR); Function f(fun); NumericMatrix y(x.ncol(),x.ncol()); for (int i = 0; i &lt; x.ncol(); i++) { for (int j = 0; j &lt; x.ncol(); j++) { y(i,j) = as&lt;double&gt;(f(wrap(x(_,i)),wrap(x(_,j)))); } } return wrap(y); ' Papply2 &lt;- cxxfunction(signature(xR="numeric",fun="function"),src,plugin="Rcpp") </code></pre> <p>But both are quite slow even on a pretty small dataset of 100 variables ( I thought the Rcpp function would be faster, but I guess conversion between R and C++ all the time takes its toll):</p> <pre><code>&gt; system.time(Papply(matrix(rnorm(100*300),300,100),function(x,y)cor.test(x,y)$p.value)) user system elapsed 3.73 0.00 3.73 &gt; system.time(Papply2(matrix(rnorm(100*300),300,100),function(x,y)cor.test(x,y)$p.value)) user system elapsed 3.71 0.02 3.75 </code></pre> <p>So my question is:</p> <ol> <li>Due to the simplicity of these functions I assume this is already somewhere in R. Is there an apply or <code>plyr</code> function that does this? I have looked for it but haven't been able to find it.</li> <li>If so, is it faster?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload