I have a 5 column matrix like 12 10 8 6 3 10 9 8 7 5 14 NA 4 NA NA NA 15 NA 10 NA 5 ... I want to select the position of the first entry for each row <=5 for example, for the first row, I want to select the last element and return its position as 5; for th e third row, I want to select the third element and return its position as 3; similarly for the 4th row, I want to select the fifth element and return its position 5. I am wondering how to do this fast? Thanks a lot! [[alternative HTML version deleted]]
On Jul 20, 2011, at 4:23 PM, gallon li wrote:> I have a 5 column matrix like > > 12 10 8 6 3 > 10 9 8 7 5 > 14 NA 4 NA NA NA > 15 NA 10 NA 5 > ...Probably something along the lines of aapply(mtx, 1, function(x) { c( x[ which(x <= 5)[1] ], # first row are the values which(x <= 5)[1]) } ) # second row the positions -- David.> > I want to select the position of the first entry for each row <=5 > > for example, for the first row, I want to select the last element > and return > its position as 5; > for th e third row, I want to select the third element and return its > position as 3; > similarly for the 4th row, I want to select the fifth element and > return its > position 5. > > I am wondering how to do this fast? Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Tena koe Assuming your matrix is called yourMatrix, then try apply(yourMatrix, 1, function(x) which(x<=5)) HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of gallon li > Sent: Thursday, 21 July 2011 8:23 a.m. > To: R-help at r-project.org > Subject: [R] select element from each row of the matrix > > I have a 5 column matrix like > > 12 10 8 6 3 > 10 9 8 7 5 > 14 NA 4 NA NA NA > 15 NA 10 NA 5 > ... > > I want to select the position of the first entry for each row <=5 > > for example, for the first row, I want to select the last element and > return > its position as 5; > for th e third row, I want to select the third element and return its > position as 3; > similarly for the 4th row, I want to select the fifth element and > return its > position 5. > > I am wondering how to do this fast? Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Looping over the rows, as David did below, is one way. You can also loop over the columns. This can be faster when nrow(matrix)>>ncol(matrix). E.g., with the following 2 functions f0 <- function (x) { apply(x, 1, function(aRow) which(aRow <= 5)[1]) } f1 <- function (x) { isGood <- !is.na(x) & x <= 5 retval <- rep(NA, nrow(x)) for (col in rev(seq_len(ncol(x)))) { retval[isGood[, col]] <- col } retval } and a 1 million row (by 5 column) matrix set.seed(1) mBig <- matrix(sample(10,size=5*10^6,replace=TRUE), ncol=5, nrow=10^6) mBig[sample(length(mBig), size=length(mBig)/10)] <- NA I get > system.time(z0 <- f0(mBig)) user system elapsed 17.84 0.02 17.36 > system.time(z1 <- f1(mBig)) user system elapsed 0.56 0.01 0.56 > identical(z0,z1) [1] TRUE In either case it can save time (and sometimes use extra memory) to compute things outside of the loop. f1 already does that but f0 would be a bit quicker if you moved some repeated calculations out of the loop: f0a <- function (x) { s <- seq_len(ncol(x)) apply(!is.na(x) & x <= 5, 1, function(aRow) s[aRow][1]) } Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius > Sent: Wednesday, July 20, 2011 1:35 PM > To: gallon li > Cc: R-help at r-project.org > Subject: Re: [R] select element from each row of the matrix > > > On Jul 20, 2011, at 4:23 PM, gallon li wrote: > > > I have a 5 column matrix like > > > > 12 10 8 6 3 > > 10 9 8 7 5 > > 14 NA 4 NA NA NA > > 15 NA 10 NA 5 > > ... > > Probably something along the lines of > > aapply(mtx, 1, function(x) { c( x[ which(x <= 5)[1] ], # first row are > the values > which(x <= 5)[1]) } ) # second row > the positions > > -- > David. > > > > > I want to select the position of the first entry for each row <=5 > > > > for example, for the first row, I want to select the last element > > and return > > its position as 5; > > for th e third row, I want to select the third element and return its > > position as 3; > > similarly for the 4th row, I want to select the fifth element and > > return its > > position 5. > > > > I am wondering how to do this fast? Thanks a lot! > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.