How to use R to create a word co-occurrence matrix

后端 未结 1 399
迷失自我
迷失自我 2021-01-13 12:45

I am a newbie in r. I have a set of data about online videos and their tags. The data looks like

film  tag1 tag2 tag3 tag4....
1      A    B    C    D
2              


        
1条回答
  •  被撕碎了的回忆
    2021-01-13 13:44

    If I understand what you want here is one way:

    dat <- read.table(text='film  tag1 tag2 tag3 tag4
    1      A    B    C    D
    2      A    C    F    G 
    3      B    D    C    X', header=T)
    
    library(qdapTools)
    crossprod(as.matrix(mtabulate(as.data.frame(t(dat[, -1])))))
    

    Giving:

      A B C D F G X
    A 2 1 2 1 1 1 0
    B 1 2 2 2 0 0 1
    C 2 2 3 2 1 1 1
    D 1 2 2 2 0 0 1
    F 1 0 1 0 1 1 0
    G 1 0 1 0 1 1 0
    X 0 1 1 1 0 0 1
    

    0 讨论(0)
提交回复
热议问题