1

I have a dataframe with unique items procured on rows and their corresponding sources on columns (Basically this is a data which I have "dcast" from original data)

Now it looks like this

DF <- data.frame(Food = c("Burger", "Pizza", "Egg rolls", "Noodles", "Tea", "Coffee", "Biscuits", "Apples", "Bananas"), Source1=c(0,0,1,1,0,0,1,0,0), Source2=c(0,0,1,0,1,0,1,0,1), Source3=c(0,0,0,0,1,0,0,0,1), Source4=c(1,0,0,1,0,0,0,0,0)

)

Now based on this dacasted data I have to create a table which checks for duplicay from sources like this

This is the desired output

Vaibhav
  • 21
  • 6

1 Answers1

1

You can get it via the following codes:

m <- as.matrix(DF[-1])
z <- t(m)%*%m
diag(z) <- NA

which gives:

> z
        Source1 Source2 Source3 Source4
Source1      NA       2       0       1
Source2       2      NA       2       0
Source3       0       2      NA       0
Source4       1       0       0      NA
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81