From: Thaden, John J <ThadenJohnJ_at_uams.edu>

Date: Fri, 11 Jul 2008 11:23:25 -0500

# user system elapsed

# 0.40 0.02 0.42

system.time(apply(m, 2, func2))

# user system elapsed

# 0.70 0.05 0.75

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 Fri 11 Jul 2008 - 17:15:28 GMT

Date: Fri, 11 Jul 2008 11:23:25 -0500

I had written asking for a simple way to extract the Index of the last value in a vector greater than some cutoff, e.g., the index, 6, for a cutoff of 20 and this example vector:

v <- c(20, 134, 45, 20, 24, 500, 20, 20, 20)

Thank you, Alain Guillet, for this simple solution sent to me offlist:

Also, thank you Lisa Readdy for a lengthier solution.

v[max(which(v > 20))] (Henrique Dallazuanna)

Jim's use of tail() suggests a variant to Alain's solution

m <- matrix(rexp(3e6,rate=0.05), nrow=600) # 5,000 cols m[m<20] <- 20

func1 <- function(v,cut=20) max(which(v>20)) func2 <- function(v,cut=20) tail(which(v>20),1) system.time(apply(m, 2, func1))

# user system elapsed

# 0.40 0.02 0.42

system.time(apply(m, 2, func2))

# user system elapsed

# 0.70 0.05 0.75

Thank you again, Alain and others.

John

On Thu, Jul 10, 2008 at 9:41 AM, John Thaden wrote:

> This shouldn't be hard, but it's just not

*> coming to me:
**> Given a vector, e.g.,
**> v <- c(20, 134, 45, 20, 24, 500, 20, 20, 20)
**> how can I get the index of the last value in
**> the vector that has a value greater than n, in
**> the example, with n > 20? I'm looking for
**> an efficient function I can use on very large
**> matrices, as the FUN argument in the apply()
**> command.
*

Confidentiality Notice: This e-mail message, including a...{{dropped:8}}

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 Fri 11 Jul 2008 - 17:15:28 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 Sat 12 Jul 2008 - 12:32:09 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.
*