1

I have a data set structured like this:

 df <- c()
 df$Var1 <- c("cat", "dog", "pig", "cat", "cat", "pig")
 df$Var2  <- c("A", "B", "C", "B", "C", "A")
 df <- as.data.frame(df)



Var1.  Var2.

cat.    A
dog.    B
pig.    C
cat.    B
cat.    C
pig.    A

With pairwise_count, I obtained a data frame made by three columns, the first and second are made by elements belonging to first column of original dataframe, the third is the number of elements in common of Var2 that the elements of Var1 have.

 df <- pairwise_count(df, Var1, Var2)

# A tibble: 4 x 3
item1 item2     n
<fct> <fct> <dbl>
1 dog   cat     1
2 pig   cat     2
3 cat   dog     1
4 cat   pig     2

I want to obtain a new dataframe, which has a fourth column, making the elements in common explicit.

 item1 item2    n  common_el

  dog   cat     1     B
  pig   cat     2     A
  pig   cat     2     C
  cat   dog     1     B
  cat   pig     2     A
  cat   pig     2     C

How can I proceed?

Thank you for your suggestions!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Silvia
  • 405
  • 4
  • 17

0 Answers0