How to convert two factors to adjacency matrix in R?

前端 未结 1 551
忘掉有多难
忘掉有多难 2021-01-24 21:32

I have a data frame with two columns (key and value) where each column is a factor:

df = data.frame(gl(3,4,labels=c(\'a\',\'b\',\'c\')), gl(6,2))
colnames(df) =          


        
相关标签:
1条回答
  • 2021-01-24 22:33

    An easy way to do it in base R:

    res <-table(df)
    res[res>0] <-1
    res
       value
    #key 1 2 3 4 5 6
    #  a 1 1 0 0 0 0
    #  b 0 0 1 1 0 0
    #  c 0 0 0 0 1 1
    
    0 讨论(0)
提交回复
热议问题