Emmanuel Levy
2012-Dec-27 19:17 UTC
[R] Retrieve indexes of the "first occurrence of numbers" in an effective manner
Hi,
That sounds simple but I cannot think of a really fast way of getting
the following:
c(1,1,2,2,3,3,4,4) would give c(1,3,5,7)
i.e., a function that returns the indexes of the first occurrences of numbers.
Note that numbers may have any order e.g., c(3,4,1,2,1,1,2,3,5), can
be very large, and the vectors are also very large (which prohibits
any loop).
The best I could think of is:
tmp = c(1,1,2,2,3,3,4,4)
u = unique(tmp)
sapply(u, function(x){ which(!is.na(match(tmp,x)))[1]} )
But there might be a useful function I don't know ..
Thanks for any hint.
All the best,
Emmanuel
Jeff Newmiller
2012-Dec-27 19:33 UTC
[R] Retrieve indexes of the "first occurrence of numbers" in an effective manner
x <- c(1,1,2,2,3,3,4,4)
match(unique(x),x)
---------------------------------------------------------------------------
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.
Emmanuel Levy <emmanuel.levy at gmail.com> wrote:
>Hi,
>
>That sounds simple but I cannot think of a really fast way of getting
>the following:
>
> c(1,1,2,2,3,3,4,4) would give c(1,3,5,7)
>
>i.e., a function that returns the indexes of the first occurrences of
>numbers.
>
>Note that numbers may have any order e.g., c(3,4,1,2,1,1,2,3,5), can
>be very large, and the vectors are also very large (which prohibits
>any loop).
>
>The best I could think of is:
>
>tmp = c(1,1,2,2,3,3,4,4)
>u = unique(tmp)
>sapply(u, function(x){ which(!is.na(match(tmp,x)))[1]} )
>
>But there might be a useful function I don't know ..
>
>Thanks for any hint.
>All the best,
>
>Emmanuel
>
>______________________________________________
>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.
Michael Weylandt
2012-Dec-27 19:35 UTC
[R] Retrieve indexes of the "first occurrence of numbers" in an effective manner
?duplicated will help. M On Dec 27, 2012, at 8:17 PM, Emmanuel Levy <emmanuel.levy at gmail.com> wrote:> Hi, > > That sounds simple but I cannot think of a really fast way of getting > the following: > > c(1,1,2,2,3,3,4,4) would give c(1,3,5,7) > > i.e., a function that returns the indexes of the first occurrences of numbers. > > Note that numbers may have any order e.g., c(3,4,1,2,1,1,2,3,5), can > be very large, and the vectors are also very large (which prohibits > any loop). > > The best I could think of is: > > tmp = c(1,1,2,2,3,3,4,4) > u = unique(tmp) > sapply(u, function(x){ which(!is.na(match(tmp,x)))[1]} ) > > But there might be a useful function I don't know .. > > Thanks for any hint. > All the best, > > Emmanuel > > ______________________________________________ > 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.
arun
2012-Dec-27 20:15 UTC
[R] Retrieve indexes of the "first occurrence of numbers" in an effective manner
Hi,
Try
?which(!duplicated(c(3,4,1,2,1,1,2,3,5)))
#[1] 1 2 3 4 9
which(!duplicated(c(1,1,2,2,3,3,4,4)))
#[1] 1 3 5 7
A.K.
----- Original Message -----
From: Emmanuel Levy <emmanuel.levy at gmail.com>
To: R-help Mailing List <r-help at r-project.org>
Cc:
Sent: Thursday, December 27, 2012 2:17 PM
Subject: [R] Retrieve indexes of the "first occurrence of numbers" in
an effective manner
Hi,
That sounds simple but I cannot think of a really fast way of getting
the following:
c(1,1,2,2,3,3,4,4) would give c(1,3,5,7)
i.e., a function that returns the indexes of the first occurrences of numbers.
Note that numbers may have any order e.g., c(3,4,1,2,1,1,2,3,5), can
be very large, and the vectors are also very large (which prohibits
any loop).
The best I could think of is:
tmp = c(1,1,2,2,3,3,4,4)
u = unique(tmp)
sapply(u, function(x){ which(!is.na(match(tmp,x)))[1]} )
But there might be a useful function I don't know ..
Thanks for any hint.
All the best,
Emmanuel
______________________________________________
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.
Maybe Matching Threads
- Idea/package to "linearize a curve" along the diagonal?
- Finding (swapped) repetitions of numbers pairs across two columns
- group bunch of lines in a data.frame, an additional requirement
- How to "flatten" a multidimensional array into a dataframe?
- which(df$name=="A") takes ~1 second! (df is very large), but can it be speeded up?