Reputation: 2106
I have 2 dataframes. First (and I know how to do this) I want to find which rows (the whole row) match between the 2 dataframes. So I can create a column in A which tells me if that entire row is in B. However, the part I don't know how to do is to find which indices in B it would be.
TL DR; I need to create a new column in A, which tells me FALSE if the whole row isn't in B, or instead give me the index of where that row is in B.
a = as.data.frame(matrix(data = 1:10,nrow=5))
b = as.data.frame(matrix(data = c(1:5,10,7,6,9,8), nrow=5))
set.seed(02138)
b = b[sample(nrow(b)),]
rownames(b) = 1:nrow(b)
a_ = do.call("paste", a[,1:2])
b_ = do.call("paste", b[,1:2])
# Gets me TRUE/FALSE of whether there is a complete row-wise match
a$InB = a_ %in% b_
# Gets me which rows they are in b
b[b_ %in% a_,]
# Now is where I need help combining this
# Expected output
> a$InB = temp
> a
V1 V2 InB
1 1 6 FALSE
2 2 7 3
3 3 8 FALSE
4 4 9 5
5 5 10 FALSE
Upvotes: 2
Views: 2032
Reputation: 28441
You can add this:
a$InB[a$InB] <- as.character(which(b_ %in% a_))
a
# V1 V2 InB
#1 1 6 FALSE
#2 2 7 3
#3 3 8 FALSE
#4 4 9 5
#5 5 10 FALSE
Upvotes: 5