AAAA
AAAA

Reputation: 473

R: compare two groups of vectors

I have made two recommendation systems and would like to compere the products they recommend and to see how many products are mutual. I joined the two results into data frame - one recommendation system columns starts with "z", other one with "b".

Example data:

df <- data.frame(z1 = c("a", "s", "d"), z2 = c("z", "x", "c"), z3 = c("q", "w", "e"),
            b1 = c("w", "a", "e"), b2 = c("a", "i", "r"), b3 = c("z", "w", "y"))

ID   z1 z2 z3   b1 b2 b3
1    a  z  q    q  a  z
2    s  x  w    a  i  r
3    d  c  e    r  e  y

Desired results:

ID   z1 z2 z3   b1 b2 b3   mutual_recommendation
1    a  z  q    q  a  z   3
2    s  x  w    a  i  r   0
3    d  c  e    e  r  y   1

The problem is that the order might not be the same and compering all the combinations is by Case or ifelse would be a lot of combination, specially when number of Top-N recommendation will change to 10.

Upvotes: 1

Views: 284

Answers (2)

mysteRious
mysteRious

Reputation: 4294

Here is another solution (note: I changed the data.frame code to produce the data frame that is actually shown under it in the question - they do not match):

> library(dplyr)
> df %>% mutate(mutual_recommendation=apply(df,1,function(x) sum(x[1:3] %in% x[4:6]) ))
  z1 z2 z3 b1 b2 b3 mutual_recommendation
1  a  z  q  q  a  z                     3
2  s  x  w  a  i  r                     0
3  d  c  e  r  e  y                     1

Upvotes: 1

akrun
akrun

Reputation: 887038

We can use an apply to loop over the rows of the subset of dataset (removed the 'ID' column), get the length of intersect of the first 3 and next 3 elements

df$mutual_recommendation <- apply(df[-1], 1, FUN = function(x) 
                        length(intersect(x[1:3], x[4:6])))
df$mutual_recommendation
#[1] 3 0 1

Upvotes: 2

Related Questions