Hello, Given a vector I would like to rapidly identify duplicated non contiguous elements. Given for example c(1, 1, 2, 3, 2, 4, 5, 6, 4) I would like to get: FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE In fact I need to check this on the columns of a matrix! I can do that of couse with loops but is there any function already available? Thanks ------------------------------------------------------ Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom http://infostrada.it
Maybe not the most efficient, but here's a vector-level solution:
non.contig.dupes <- function(x){
is.dup <- x %in% x[duplicated(x)]
is.con <- c(x[-length(x)]==x[-1],F) | c(F,x[-length(x)]==x[-1])
is.dup & !is.con}
On Mar 16, 2007, at 11:14 AM, Bruno C.. wrote:
> Hello,
>
> Given a vector I would like to rapidly identify duplicated non
> contiguous elements.
> Given for example
> c(1, 1, 2, 3, 2, 4, 5, 6, 4)
> I would like to get:
> FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE
>
> In fact I need to check this on the columns of a matrix!
> I can do that of couse with loops but is there any function already
> available?
>
> Thanks
>
>
> ------------------------------------------------------
> Passa a Infostrada. ADSL e Telefono senza limiti e senza canone
> Telecom
> http://infostrada.it
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Many thanks for the fast reply of Leeds
Here's all the anwer I got:
Here's the winner: Patrick Burns
with:
* any(duplicated(rle(x)$values))
followed by:
Peter McMahan:
non.contig.dupes <- function(x){
is.dup <- x %in% x[duplicated(x)]
is.con <- c(x[-length(x)]==x[-1],F) | c(F,x[-length(x)]==x[-1])
is.dup & !is.con}
then mine (size was 6 just for test):
function (z) {zz<-t(z);risp<-matrix(FALSE,1,6);
for (i in c(1,2,3,4,5,6)){
if (
(zz[i]%in%zz[-c(max(0,i-1),i,min(i+1,6))]))
&&
(
( (i>1) &&(i<6)&& ((zz[i]!=zz[i-1])|| (zz[i]!=zz[i+1]) ))
||
( (i==1) && (zz[i]!=zz[i+1]))
||
( (i==6) && (zz[i]!=zz[i-1]))
)
)
{
risp[1,i]=TRUE;
}
}
risp;
}
aex-equo with Mark Leeds whose script does not work for all the situations:
temp<-diff(inmat)
temp<-temp[temp != 0]
duplicated(temp)
Many thanks to All of You
------------------------------------------------------
Passa a Infostrada. ADSL e Telefono senza limiti e senza canone Telecom
http://infostrada.it
[[alternative HTML version deleted]]