Is there a R function that applies a function to each pair of columns?

后端 未结 4 2131
小鲜肉
小鲜肉 2020-11-27 05:35

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 mak

4条回答
  •  悲哀的现实
    2020-11-27 05:54

    92% of the time is being spent in cor.test.default and routines it calls so its hopeless trying to get faster results by simply rewriting Papply (other than the savings from computing only those above or below the diagonal assuming that your function is symmetric in x and y).

    > M <- matrix(rnorm(100*300),300,100)
    > Rprof(); junk <- Papply(M,function(x,y) cor.test( x, y)$p.value); Rprof(NULL)
    > summaryRprof()
    $by.self
                     self.time self.pct total.time total.pct
    cor.test.default      4.36    29.54      13.56     91.87
    # ... snip ...
    

提交回复
热议问题