You don't need the apply call either. Here's a version without it. With only
30 rows, the apply is just fine; actually a tad faster than my version. If you
were doing this thing on a 10,000 x 30 matrix instead of 30 x 10,000, dumping
the apply call could save a lot of time.
Cheers,
Scott
Scott D Chasalow
Bristol-Myers Squibb
"leadin1" <-
function(mat = start.matrix)
{
rowcol <- function(pos, nr, nc) {
cc <- (pos - 1.) %/% nr
rr <- pos - (nr * cc)
cc <- cc + 1.
cbind(row = rr, column = cc)
}
d <- dim(mat)
nr <- d[1]
nc <- d[2]
pat <- rep(c(1, 0), nr)
tt <- rowcol(which(mat == 1), nr, nc)
tt <- tt[order(tt[, 1], tt[, 2]), , drop = FALSE]
tt <- tt[!duplicated(tt[, 1]), , drop = FALSE]
terminals <- numeric(nr)
terminals[tt[, 1]] <- tt[, 2]
copy.numbers <- as.numeric(rbind(terminals, nc - terminals))
t(array(rep(pat, copy.numbers), c(nc, nr)))
}
> -------- Original Message --------
> Subject: Re: [S] matrix manipulation question
> Date: Wed, 20 Jul 2005 12:21:08 -0400 (EDT)
> From: jsv@stat.ohio-state.edu
> To: David L Lorenz <lorenz@usgs.gov>
> CC: Felipe <felipe.jordao@moodys.com>, s-news@lists.biostat.wustl.edu,
> s-news-owner@lists.biostat.wustl.edu
> References:
> <CDBE3ADAFCB39F41A19FF2BE650978FF3D041E@mdynycmsx03.ad.moodys.net>
> <OFBBC81361.208A05FB-ON86257044.0057E7FA-86257044.00582158@usgs.gov>
>
> The only problem is if there is more than one "1" in a row:
> Here's the same idea, with the fix:
>
> leadin1 <- function(mat=start.matrix){
> nr <- nrow(mat)
> nc <- ncol(mat)
> pat <- rep(c(1,0),nr)
> terminals <- apply(mat,1,function(x){match(1,x,nomatch=0)[1]})
> copy.numbers <- as.numeric(rbind(terminals,nc-terminals))
> out.mat <- matrix(rep(pat,copy.numbers),nr,nc,byrow=T)
> out.mat
> }
>
> ______________________
>
>
>
> > Felipe,
> > Use apply instead of a for loop. Here's an example of a function that
> > should work. Note that apply returns a matrix that would need to be
> > transposed.
> >
> > apply(matrix(c(
> > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> > 0,0,0,0,1,2,3,0,0,0,0,0,0,0,0,0,0,
> > 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
> > 4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> > 2,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
> > 1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,
> > 0,0,0,0,0,0,1,2,3,4,5,0,0,0,0,0,0), ncol=17, byrow=T), 1,
> > FUN = function(x) {
> > one <- match(1, x, nomatch=0)
> > return(c(rep(1,one), rep(0,length(x)-one)))
> > })
> >
> > Dave
> >
> >
> >
> >
> > Jordão, Felipe <Felipe.Jordao@moodys.com>
> > Sent by: s-news-owner@lists.biostat.wustl.edu
> > 07/20/2005 10:16 AM
> >
> > To: <s-news@lists.biostat.wustl.edu>
> > cc:
> > Subject: [S] matrix manipulation question
> >
> >
> >
> > Hi all,
> >
> > I have a matrix with some numbers (0:5). The number sequence usually
> > begins with 1, but if starts in column 1 it can begin with another number.
> > Once started it will go sequentially with no 0s in the middle, then go
> > back to 0s. I want to do the following for each row:
> >
> > 1. If the sum of the row is zero, leave it as all 0s (i.e. it's blank to
> > begin with, then leave it blank)
> > 2. If the sum of the row is not zero, continue and do the following
> > 3. Fill it with 1s until I hit the first "1", fill it with zeroes
> > thereafter
> >
> > Here is an example:
> >
> > Start Matrix
> > 00000000000000000
> > 00001230000000000
> > 00000000100000000
> > 45000000000000000
> > 23400000000000000
> > 12345000000000000
> > 00000012345000000
> > ...
> >
> > End Matrix
> > 00000000000000000
> > 11111000000000000
> > 11111111100000000
> > 00000000000000000
> > 00000000000000000
> > 10000000000000000
> > 11111110000000000
> > ...
> >
> > It is a large enough matrix that I get a memory error if I use for loops
> > (about 30x10000). I also need to keep the ordering of the rows, so if I
> > start doing things like breaking it up in chunks it has to be put back
> > properly.
> >
> > I appreciate any suggestions.
> >
> > thanks in advance,
> > Felipe
> >
> >
> >
> >
> > ---------------------------------------
> >
> > The information contained in this e-mail message, and any attachment
> > thereto, is confidential and may not be disclosed without our express
> > permission. If you are not the intended recipient or an employee or agent
> > responsible for delivering this message to the intended recipient, you are
> > hereby notified that you have received this message in error and that any
> > review, dissemination, distribution or copying of this message, or any
> > attachment thereto, in whole or in part, is strictly prohibited. If you
> > have received this message in error, please immediately notify us by
> > telephone, fax or e-mail and delete the message and all of its
> > attachments. Thank you.
> >
> > Every effort is made to keep our network free from viruses. You should,
> > however, review this e-mail message, as well as any attachment thereto,
> > for viruses. We take no responsibility and have no liability for any
> > computer virus which may be transferred via this e-mail message.
> >
> > --------------------------------------------------------------------
> > This message was distributed by s-news@lists.biostat.wustl.edu. To
> > unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> > the BODY of the message: unsubscribe s-news
> >
> >
>
>
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu. To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message: unsubscribe s-news
|