Message-ID: <728b3945-333c-36f7-d385-480c5cdb8926@gmail.com>
Date: 2018-11-13T10:15:40Z
From: Duncan Murdoch
Subject: which element is duplicated?
In-Reply-To: <83a637dc-2605-fa4f-6a3a-8949f85e2122@fredhutch.org>
On 13/11/2018 12:35 AM, Pages, Herve wrote:
> Hi,
>
> On 11/12/18 17:08, Duncan Murdoch wrote:
>> The duplicated() function gives TRUE if an item in a vector (or row in
>> a matrix, etc.) is a duplicate of an earlier item.? But what I would
>> like to know is which item does it duplicate?
>>
>> For example,
>>
>> v <- c("a", "b", "b", "a")
>> duplicated(v)
>>
>> returns
>>
>> [1] FALSE FALSE? TRUE? TRUE
>>
>> What I want is a fast way to calculate
>>
>> ?[1] NA NA 2 1
>>
>> or (equally useful to me)
>>
>> ?[1] 1 2 2 1
>>
>> The result should have the property that if result[i] == j, then v[i]
>> == v[j], at least for i != j.
>>
>> Does this already exist somewhere, or is it easy to write?
>
> I generally use match() for that:
>
> > v <- c("a", "b", "b", "a")
>
> > match(v, v)
>
> [1] 1 2 2 1
Yes, this is perfect. Thanks to you (and the private answer I received
that suggested the same).
Duncan Murdoch