From: Lars Bishop <lars52r_at_gmail.com>

Date: Tue, 26 Apr 2011 08:01:46 -0400

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. Received on Tue 26 Apr 2011 - 12:08:23 GMT

Date: Tue, 26 Apr 2011 08:01:46 -0400

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 <- sapply(1:length(sec.large), function(i) a[i, sec.large[i]])
ans

Thanks in advance for your help,

Lars.

[[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. Received on Tue 26 Apr 2011 - 12:08:23 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 Tue 26 Apr 2011 - 13:30:32 GMT.

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