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 having a value greater than n, in this case, greater than 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}}
Try this: v[max(which(v > 20))] On Thu, Jul 10, 2008 at 9:41 AM, Thaden, John J <ThadenJohnJ at uams.edu> 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 having a value greater than n, in > this case, greater than 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Here is one way:> v <- c(20, 134, 45, 20, 24, 500, 20, 20, 20) > tail(v[v>20],1)[1] 500 On Thu, Jul 10, 2008 at 8:41 AM, Thaden, John J <ThadenJohnJ at uams.edu> 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 having a value greater than n, in > this case, greater than 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?