Matching rows to columns and counting same occurences R

Viewed 67

I have a dataset which is of the following form:-

a <- data.frame(X1=c("A", "B", "C", "A", "B", "C"),
                X2=c("B", "C", "C", "A", "A", "B"),
                X3=c("B", "E", "A", "A", "A", "B"),
                X4=c("E", "C", "A", "A", "A", "C"),
                X5=c("A", "C", "C", "A", "B", "B")
               )

And I have another set of the following form:-

b <- data.frame(col_1=c("ASD", "ASD", "BSD", "BSD"),
                col_2=c(1, 1, 1, 1),
                col_3=c(12, 12, 31, 21),
                col_4=("A", "B", "B", "A")
               )

What I want to do is to take the column col_4 from set b and match row wise in set a, so that it tell me which row has how many elements from col_4 in a new column. The name of the new column does not matters.

For ex:- The first and fifth row in set a has all the elements of col_4 from set b.

Also, duplicates shouldn't be found. For ex. sixth row in set a has 3 "B"s. But since col_4 from set b has only two "B"s, it should tell me 2 and not 3.

Expected output is of the form:-

c <- data.frame(X1=c("A", "B", "C", "A", "B", "C"),
                X2=c("B", "C", "C", "A", "A", "B"),
                X3=c("B", "E", "A", "A", "A", "B"),
                X4=c("E", "C", "A", "A", "A", "C"),
                X5=c("A", "C", "C", "A", "B", "B"),
                found=c(4, 1, 2, 2, 4, 2)
               )
3 Answers

We can use vecsets::vintersect which takes care of duplicates.

Using apply row-wise we can count how many common values are there between b$col4 and each row in a.

apply(a, 1, function(x) length(vecsets::vintersect(b$col_4, x)))
#[1] 4 1 2 2 4 2

An option using data.table:

library(data.table)

#convert a into a long format
m <- melt(setDT(a)[, rn:=.I], id.vars="rn", value.name="col_4")

#order by row number and create an index for identical occurrences in col_4
setorder(m, rn, col_4)[, vidx := rowid(col_4), rn]

#create a similar index for b
setDT(b, key="col_4")[, vidx := rowid(col_4)]

#count occurrences and lookup this count into original data
a[b[m, on=.(col_4, vidx), nomatch=0L][, .N, rn], on=.(rn), found := N]

output:

   X1 X2 X3 X4 X5 rn found
1:  A  B  B  E  A  1     4
2:  B  C  E  C  C  2     1
3:  C  C  A  A  C  3     2
4:  A  A  A  A  A  4     2
5:  B  A  A  A  B  5     4
6:  C  B  B  C  B  6     2

Another idea to operate on sets efficiently is to count and compare the element occurences of b$col_4 in each row of a:

b1 = c(table(b$col_4))
#b1
#A B 
#2 2

a1 = table(factor(as.matrix(a), names(b1)), row(a))
#a1
#   
#    1 2 3 4 5 6
#  A 2 0 2 5 3 0
#  B 2 1 0 0 2 3

Finally, identify the least amount of occurences per element (for each row) and sum:

colSums(pmin(a1, b1))
#1 2 3 4 5 6 
#4 1 2 2 4 2

In case of a larger dimension a "data.frame" and more elements, Matrix::sparseMatrix offers an appropriate alternative:

library(Matrix)

a.fac = factor(as.matrix(a), names(b1))
.i = as.integer(a.fac)
.j = c(row(a))

noNA = !is.na(.i)  ## need to remove NAs manually
.i = .i[noNA]
.j = .j[noNA]

a1 = sparseMatrix(i = .i, j = .j, x = 1L, dimnames = list(names(b1), 1:nrow(a)))

a1
#2 x 6 sparse Matrix of class "dgCMatrix"
#  1 2 3 4 5 6
#A 2 . 2 5 3 .
#B 2 1 . . 2 3

colSums(pmin(a1, b1))
#1 2 3 4 5 6 
#4 1 2 2 4 2
Related