Reshape dataframe and create similarity matrix

前端 未结 2 1383
野性不改
野性不改 2021-01-22 04:10

I have a data table, and I try Reshaping it but it doesn\'t work, how do I do this:

I have a data table:

Name | Value
-------------
Bob  | 8,9,10
-------         


        
相关标签:
2条回答
  • 2021-01-22 04:55
     res <- read.table(text="Name | Value
     Bob  | 8,9,10
     Mike | 2,3,4
     Sandr| 5,6,7", header=TRUE, sep="|")
    dres <- data.frame(Value= unlist( strsplit(as.character(res$Value), ",") )
    , Name=rep(res$Name, each=3))
    dres <- dres[order(as.numeric(as.character(dres$Value))), ]
    dres
    outer(sort(dres$Value), sort(dres$Value), FUN=function(x,y) dres[x, "Name"] == dres[y,"Name"] )
           [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]
     [1,]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
     [2,]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
     [3,]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
     [4,] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
     [5,] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
     [6,] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
     [7,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
     [8,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
     [9,] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE
    
    0 讨论(0)
  • 2021-01-22 05:01

    The functions you are looking for are stack and contrasts.

    data<-list(bob=c(8,9,10),mike=c(2,3,4),sandr=c(5,6,7))
    
    as.data.frame(data)
    
      bob mike sandr
    1   8    2     5
    2   9    3     6
    3  10    4     7
    
    stack(data)
    
      values   ind
    1      8   bob
    2      9   bob
    3     10   bob
    4      2  mike
    5      3  mike
    6      4  mike
    7      5 sandr
    8      6 sandr
    9      7 sandr
    
    df<-stack(data)
    contrasts(df$ind,contrasts=FALSE)[df$ind,df$ind]
    
          bob bob bob mike mike mike sandr sandr sandr
    bob     1   1   1    0    0    0     0     0     0
    bob     1   1   1    0    0    0     0     0     0
    bob     1   1   1    0    0    0     0     0     0
    mike    0   0   0    1    1    1     0     0     0
    mike    0   0   0    1    1    1     0     0     0
    mike    0   0   0    1    1    1     0     0     0
    sandr   0   0   0    0    0    0     1     1     1
    sandr   0   0   0    0    0    0     1     1     1
    sandr   0   0   0    0    0    0     1     1     1
    

    You can assign row names and column names and sort if desired

    im<-contrasts(df$ind,contrasts=FALSE)[df$ind,df$ind] 
    rownames(im)<-df$values
    colnames(im)<-df$values
    
    0 讨论(0)
提交回复
热议问题