Re: [R] Second largest element from each matrix row

2011-04-26 Thread Peter Ehlers
p-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of peter dalgaard Sent: Tuesday, April 26, 2011 8:13 AM To: David Winsemius Cc: r-help@r-project.org Subject: Re: [R] Second largest element from each matrix row On Apr 26, 2011, at 14:36 , David Winsemius wrote: On Apr 26, 2

Re: [R] Second largest element from each matrix row

2011-04-26 Thread William Dunlap
@r-project.org] On Behalf Of William Dunlap > Sent: Tuesday, April 26, 2011 9:11 AM > To: peter dalgaard; David Winsemius > Cc: r-help@r-project.org > Subject: Re: [R] Second largest element from each matrix row > > A different approach is to use order() to sort > first

Re: [R] Second largest element from each matrix row

2011-04-26 Thread William Dunlap
iginal Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of peter dalgaard > Sent: Tuesday, April 26, 2011 8:13 AM > To: David Winsemius > Cc: r-help@r-project.org > Subject: Re: [R] Second largest element from each matrix row &g

Re: [R] Second largest element from each matrix row

2011-04-26 Thread peter dalgaard
On Apr 26, 2011, at 14:36 , David Winsemius wrote: > > On Apr 26, 2011, at 8:01 AM, Lars Bishop wrote: > >> Hi, >> >> I need to extract the second largest element from each row of a >> matrix. Below is my solution, but I think there should be a more efficient >> way to accomplish the same, or

Re: [R] Second largest element from each matrix row

2011-04-26 Thread Lars Bishop
Thanks a lot to all of you! On Tue, Apr 26, 2011 at 10:34 AM, Peter Ehlers wrote: > On 2011-04-26 05:26, Dimitris Rizopoulos wrote: > >> one way is the following: >> >> a<- matrix(rnorm(9), 3 ,3) >> >> aa<- a[order(row(a), -a)] >> matrix(aa, nrow(a), byrow = TRUE)[, 2] >> > > That's clever, Dmit

Re: [R] Second largest element from each matrix row

2011-04-26 Thread Peter Ehlers
On 2011-04-26 05:26, Dimitris Rizopoulos wrote: one way is the following: a<- matrix(rnorm(9), 3 ,3) aa<- a[order(row(a), -a)] matrix(aa, nrow(a), byrow = TRUE)[, 2] That's clever, Dmitris. And very fast! Lars: my apologies; I didn't read your request carefully. You asked for more _efficient

Re: [R] Second largest element from each matrix row

2011-04-26 Thread David Winsemius
On Apr 26, 2011, at 8:01 AM, Lars Bishop wrote: Hi, I need to extract the second largest element from each row of a matrix. Below is my solution, but I think there should be a more efficient way to accomplish the same, or not? set.seed(1) a <- matrix(rnorm(9), 3 ,3) sec.large <- as.vector

Re: [R] Second largest element from each matrix row

2011-04-26 Thread Peter Ehlers
On 2011-04-26 05:01, Lars Bishop wrote: Hi, I need to extract the second largest element from each row of a matrix. Below is my solution, but I think there should be a more efficient way to accomplish the same, or not? set.seed(1) a<- matrix(rnorm(9), 3 ,3) sec.large<- as.vector(apply(a,

Re: [R] Second largest element from each matrix row

2011-04-26 Thread Santosh Srinivas
would this work (shorter)? apply(a, 1, function(x) x[order(x)[2]]) On Tue, Apr 26, 2011 at 5:31 PM, Lars Bishop wrote: > Hi, > > I need to extract the second largest element from each row of a > matrix. Below is my solution, but I think there should be a more efficient > way to accomplish the s

Re: [R] Second largest element from each matrix row

2011-04-26 Thread Dimitris Rizopoulos
one way is the following: a <- matrix(rnorm(9), 3 ,3) aa <- a[order(row(a), -a)] matrix(aa, nrow(a), byrow = TRUE)[, 2] I hope it helps. Best, Dimitris On 4/26/2011 2:01 PM, Lars Bishop wrote: Hi, I need to extract the second largest element from each row of a matrix. Below is my solution

[R] Second largest element from each matrix row

2011-04-26 Thread Lars Bishop
Hi, I need to extract the second largest element from each row of a matrix. Below is my solution, but I think there should be a more efficient way to accomplish the same, or not? set.seed(1) a <- matrix(rnorm(9), 3 ,3) sec.large <- as.vector(apply(a, 1, order, decreasing=T)[2,]) ans <- sappl