Intersection of rows

Hi. I would like a vector showing the number of intersections of each row of a and b, but I can't quite get it to work. Thank you.

z <- vector()
a <- data.frame(rbind(x=c(1,6,7), y=c(1,2,8), z=c(1,2,3)))
b <- data.frame(rbind(x=c(1,5,7,8,9,10), y=c(1,2,7,8,9,10), z=c(1,2,3,8,9,10)))

inc <- function(x, y) length(intersect(a, b))
z <- inc(a,b)

You can do:

vapply(1L:nrow(a), function(i) {
  length(intersect(a[i, ], b[i, ]))
}, FUN.VALUE = integer(1L))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.