Mike Spam
2012-Sep-19 14:20 UTC
[R] effective way to return only the first argument of "which()"
Hi, I was looking for a function like "which()" but only returns the first argument. Compare: x <- c(1,2,3,4,5,6) y <- 4 which(x>y) returns: 5,6 which(x>y)[1] returns: 5 which(x>y)[1] is exactly what i need. I did use this but the dataset is too big (~18 mio. Points). That's why i need a more effective way to get the first element of a vector which is bigger/smaller than a specific number. I found "match()" but this function only works for equal numbers. Thanks, Nico
R. Michael Weylandt
2012-Sep-19 15:03 UTC
[R] effective way to return only the first argument of "which()"
On Wed, Sep 19, 2012 at 3:20 PM, Mike Spam <ichmagspam at googlemail.com> wrote:> Hi, > > I was looking for a function like "which()" but only returns the first argument. > Compare: > > x <- c(1,2,3,4,5,6) > y <- 4 > which(x>y) > > returns: > 5,6 > > which(x>y)[1] > returns: > 5 > > which(x>y)[1] is exactly what i need. I did use this but the dataset > is too big (~18 mio. Points). > That's why i need a more effective way to get the first element of a > vector which is bigger/smaller than a specific number. > > I found "match()" but this function only works for equal numbers. >It's long felt a little hack-ish but you can actually use which.min() or which.max() on logical vectors for just this purpose. x <- sample(20) which.max(x < 5) # first x satisfying the condition. Cheers, Michael> > > Thanks, > Nico > > ______________________________________________ > 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.
Jeff Newmiller
2012-Sep-19 15:05 UTC
[R] effective way to return only the first argument of "which()"
?which.max
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
Mike Spam <ichmagspam at googlemail.com> wrote:
>Hi,
>
>I was looking for a function like "which()" but only returns the
first
>argument.
>Compare:
>
>x <- c(1,2,3,4,5,6)
>y <- 4
>which(x>y)
>
>returns:
>5,6
>
>which(x>y)[1]
>returns:
>5
>
>which(x>y)[1] is exactly what i need. I did use this but the dataset
>is too big (~18 mio. Points).
>That's why i need a more effective way to get the first element of a
>vector which is bigger/smaller than a specific number.
>
>I found "match()" but this function only works for equal numbers.
>
>
>
>Thanks,
>Nico
>
>______________________________________________
>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.
Mike Spam
2012-Sep-19 15:17 UTC
[R] effective way to return only the first argument of "which()"
Hi,
Thanks Michael, but i think this is even slower.
x <-sample(20000000)
which(x < 5)[1]
which.max(x < 5)
system.time(for(i in 1:100) which.max(x < 5))
User System verstrichen
60.84 13.70 86.33
system.time(for(i in 1:100) which(x < 5)[1])
User System verstrichen
40.45 8.25 48.95
Thanks,
Nico