Reshape dataframe and create similarity matrix

前端 未结 2 1384
野性不改
野性不改 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
    

提交回复
热议问题