0

I am trying to make a cross reference table from a dataset in long format with IDs that have registered preferences of flower species. The cross reference table should list all the flower vertically and horizontally and for each flower show the frequency of likes of other flowers based upon the dataset.

dat <- data.frame(
  ID=rep(1:5, c(3,3,2,1,4)),
  Flower=c("Azalea","Begonia","Buttercup","Rose","Sunflower")[
    c(4,2,5,2,1,3,4,3,1,5,4,3,2)
  ]
)

#   ID    Flower
#1   1      Rose
#2   1   Begonia
#3   1 Sunflower
#4   2   Begonia
#5   2    Azalea
#6   2 Buttercup
#7   3      Rose
#8   3 Buttercup
#9   4    Azalea
#10  5 Sunflower
#11  5      Rose
#12  5 Buttercup
#13  5   Begonia

Intended output:

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Joe
  • 1
  • 1
  • 1
    Can you provide a simple dataset and desired output to ilustrate your problem? – Wojciech Książek Dec 08 '16 at 21:58
  • Thanks for responding to the request for the data, but the image is not very helpful. We would have to type it all in again. Instead, please use the function `dput` to create a text version that we can just cut and paste. – G5W Dec 08 '16 at 22:08
  • @G5W - I did the edit on behalf of OP - the intended output is the only part that is an image now. That should be sufficient to answer the question easily. – thelatemail Dec 08 '16 at 22:10
  • http://stackoverflow.com/questions/36111717/create-a-matrix-of-available-pairings-in-a-dataframe – rawr Dec 08 '16 at 22:18

1 Answers1

2

One straight forward solution is to use crossprod()/tcrossprod() with table():

crossprod(table(dat))

#           Flower
#Flower      Azalea Begonia Buttercup Rose Sunflower
#  Azalea         2       1         1    0         0
#  Begonia        1       3         2    2         2
#  Buttercup      1       2         3    2         1
#  Rose           0       2         2    3         2
#  Sunflower      0       2         1    2         2
Psidom
  • 209,562
  • 33
  • 339
  • 356