HI,
Replacing seq_along() with which() slightly improved CPU time.
system.time({
set.seed(1)
A<-sample(1:5,1e6,replace=TRUE)
which(!duplicated(A))
A[which(!duplicated(A))]
})
# user system elapsed
#0.040 0.012 0.052
A.K.
- Original Message -
From: Bronwyn Rayfield
T
HI,
I was thinking about duplicated(). But, Bert already posted the solution. The
solution below is not very efficient.
A<-c(9,2,9,5)
unik<-as.numeric(names(table(A)))
match(unik,A)
#[1] 2 4 1
#Bert's solution wins here.
system.time({
set.seed(1)
A<-sample(1:5,1e6,replace=TRUE)
unik <- !duplic
Here are two methods:
> A<-c(9,2,9,5)
> f1 <- function(x) { d <- !duplicated(x) ; data.frame(uniqueValue=x[d],
> firstIndex=which(d)) }
> f2 <- function(x) { u <- unique(x) ; data.frame(uniqueValue=u,
> firstIndex=match(u, x))}
> f1(A)
uniqueValue firstIndex
1 9 1
2
On 8/28/2012 5:52 PM, Bert Gunter wrote:
Sheesh!
I would have thought that someone would have noticed that on the
?unique Help page there is a link to ?duplicated, which gives a
_logical_ vector of the duplicates. From this, everything else can be
quickly derived -- and packaged in a simple Matl
Sheesh!
I would have thought that someone would have noticed that on the
?unique Help page there is a link to ?duplicated, which gives a
_logical_ vector of the duplicates. From this, everything else can be
quickly derived -- and packaged in a simple Matlab like function, if
you insist on that. e.
Hi,
Try this:
order(A)[!duplicated(sort(A))]
--
Noia Raindrops
noia.raindr...@gmail.com
__
R-help@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
On Tue, Aug 28, 2012 at 2:58 PM, Bronwyn Rayfield
wrote:
> I would like to efficiently find the first index of each unique value in a
> very large vector.
>
> For example, if I have a vector
>
> A<-c(9,2,9,5)
>
> I would like to return not only the unique values (2,5,9) but also their
> first indi
7 matches
Mail list logo