Home > Enterprise >  How to convert a 2 variables dataset into square matrix?
How to convert a 2 variables dataset into square matrix?

Time:11-27

enter image description here

I don't know if it is possible but I need to have a square matrix (8x8) in which I can see how many "source" are shared among "target" (8 because the classes of "target" variable are 8) If you can see in the image attached, there are "source" duplicates that follow different target and I would like to have a matrix that shows clearly how many source each couple of target share.

I hope I've been clear! Thanks in advance.

       | target1  | target2  | target3| ....

| -------- | -------- |---------- ------- | target1 | 999 |.... | target2 | 999 | | target3 | 999 | . . .

CodePudding user response:

@akrun this is the actual situation , now I need to convert it into a square matrix in order to see how many times, for example, both "avvenire" and "manifesto" are 1.

CodePudding user response:

Using the image and description you provided in the question, I think your data should look like this example df I made. If this is correct then you should have 64 values for each target-source that you want to generate a 8*8 matrix tidyr::pivot_wider does the work.

library(tidyr)
df = data.frame(value = 1:64,
                target = rep(LETTERS[1:8],8),
                source = rep(letters[1:8],each=8))
pivot_wider(df, names_from = target, values_from = value)
  • Related