[Rd] Suggestion: Adding quick rowMin and rowMax functions to base package

From: Sebastian Kranz <skranz_at_uni-bonn.de>
Date: Mon, 29 Mar 2010 21:34:56 +0200


Hi,

I wonder whether similarly to the very quick rowSums and colSums functions in the base package, one could add quick functions that calculate the min or max over rows / cols in a matrix. While apply(x,1,min) works, I found out by profiling a program of mine that it is rather slow for matrices with a very large number of rows. A quick functionality seems to be already there in the functions pmax and pmin, but it is rather cumbersume to apply them to all columns of a matrix (if one does not know how many columns the matrix has). Below, I have some code that shows a very unelegant implementation that illustrates possible speed gains if apply could be avoided:

rowMin = function(x) {

    # Construct a call pmin(x[,1],x[,2],...x[,NCOL(x)])

     code = paste("x[,",1:(NCOL(x)),"]",sep="",collapse=",")
     code = paste("pmin(",code,")")
     return(eval(parse(text=code)))

}

# Speed comparison: Taking rowMin of a 1,000,000 x 10 matrix
x = matrix(rnorm(1e7),1e6,10)

# The traditional apply method

y=apply(x,1,min) # Runtime ca. 12 seconds

# My unelegant rowMin function

z=rowMin(x) # Runtime ca 0.5 seconds

Of course, the way the function rowMin is constructed is highly ineffective if the matrix x has many columns, but maybe there is a simple way to adapt the code from pmin and pmax to create quick rowMin, rowMax,... functions. I don't know whether it is worth the effort, but I guess taking minima and maxima over rows is a common task.

Best wishes,
Sebastian



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 30 Mar 2010 - 06:27:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 31 Mar 2010 - 10:11:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive