[19025] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] Summary : Removing Rows from a matrix

daemon@ATHENA.MIT.EDU (Bill Dunlap)
Tue Aug 24 12:05:44 1999

Date: Tue, 24 Aug 1999 08:57:38 -0700 (PDT)
From: Bill Dunlap <bill@statsci.com>
To: "Brian A Millen." <millen.2@osu.edu>
Cc: s-news@wubios.wustl.edu
In-Reply-To: <3.0.6.32.19990823202924.007ba570@stat.ohio-state.edu>
Message-Id: <Pine.GSO.3.96.990824083234.21223B-100000@jaques>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII


On Mon, 23 Aug 1999, Brian A Millen. wrote that 2 ways to paste together
the columns of a data frame or matrix into one column for match() to use
were:

> >        	if(!is.data.frame(X))
> >                	X.cols <- split(X, col(X))
> >        	else X.cols <- X
> >        	x <- do.call("paste", c(list(sep = ","), X.cols))

and

> >xs <- apply(x,1,function(x)paste(x, collapse = ","))

Note that the first calls paste() once, giving it the columns of the data
frame as arguments while the 2nd calls paste() nrow(data) times, giving
it scalars on each call.  Because Splus function calls are fairly expensive
the former will generally be faster when the data frame has more rows
than columns (which is typical of statistical datasets).

E.g., in Splus [34].x with a 2500 by 4 matrix:

> cpu.time<-function(...)
	if (platform()=="WIN386") dos.time(...) else sum(unix.time(...)[1:2])
> x<-matrix(1:10000, ncol=4)
> cpu.time( x1<-apply(x,1,function(x)paste(x, collapse = ",")) )
[1] 1.07
> cpu.time( x2<-do.call("paste", c(list(sep=","), split(x, col(x))) ))
[1] 0.1099987
> cpu.time( x3 <- paste(sep=",", x[,1], x[,2], x[,3], x[,4]) )
[1] 0.05999947

The last is fastest, but the 2nd is better for a general function
in which you want to be able to handle datasets with an arbitrary
number of columns.  If x were a dataframe the call to split would
not be needed since data frame are stored as a list of the columns
of x, just what do.call() wants:

> xdf<-data.frame(x)
> cpu.time(x4<-do.call("paste", c(list(sep=","), xdf)))
[1] 0.06999969

You can take advantage of the column/row duality between
"+" and "sum", "|" and "any", or ">" and "max" just as we
did with paste(collapse=",",...) and paste(sep=",",...) above.
E.g., see the pmax() function.  In recent versions of Splus there
is a rowSums() function for the first but it does things in C
(which is faster still).

In general making a few function calls with long inputs is faster
than making lots of function calls with short inputs. 

----------------------------------------------------------------------------
Bill Dunlap                                      22461 Mt Vernon-Big Lake Rd
Data Analysis Products Div. of MathSoft, Inc.    Mount Vernon, WA 98274
bill@statsci.com                                 360-428-8146

"All statements in this message represent the opinions of the author and do
not necessarily reflect MathSoft policy or position."

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post