Michael Schubert
Michael Schubert

Reputation: 2796

Matching without replacement by id in R

In R, I can easily match unique identifiers using the match function:

match(c(1,2,3,4),c(2,3,4,1))
# [1] 4 1 2 3

When I try to match non-unique identifiers, I get the following result:

match(c(1,2,3,1),c(2,3,1,1))
# [1] 3 1 2 3

Is there a way to match the indices "without replacement", that is, each index appearing only once?

othermatch(c(1,2,3,1),c(2,3,1,1))
# [1] 3 1 2 4 # note the 4 where there was a 3 at the end

Upvotes: 3

Views: 419

Answers (2)

TheComeOnMan
TheComeOnMan

Reputation: 12905

A more naive approach -

library(data.table)

a <- data.table(p = c(1,2,3,1))
a[,indexa := .I]

b <- data.table(q = c(2,3,1,1))
b[,indexb := .I]

setkey(a,p)
setkey(b,q)

# since they are permutation, therefore cbinding the ordered vectors should return ab with ab[,p] = ab[,q]
ab <- cbind(a,b)
setkey(ab,indexa)
ab[,indexb]
#[1] 3 1 2 4

Upvotes: 2

Ricardo Saporta
Ricardo Saporta

Reputation: 55390

you're looking for pmatch

 pmatch(c(1,2,3,1),c(2,3,1,1))
 #  [1] 3 1 2 4

Upvotes: 4

Related Questions