Home > front end >  Create dissimilarity matrix from data.frame with three columns
Create dissimilarity matrix from data.frame with three columns

Time:04-20

This data.frame contains the dissimilarity y between two communities comm1 and comm2. I would like to convert it to a dissimilarity matrix of type dist.

library(tidyverse)
library(RcppAlgos)

k<-10 
comm_code<-seq(1,k,by=1)
data<-comboGrid(comm_code,comm_code, repetition = F)                      
data<-data.frame(data)
data<- data %>% rename(comm1=Var1,comm2=Var2)
rm(comm_code,k)
data$y<-rnorm(nrow(data),10,2)

How can I do this?

Thanks

CodePudding user response:

You can actually create it yourself:

structure(data$y, Size = 10, Labels = 1:10, method = "user", class = "dist")

           1         2         3         4         5         6         7         8         9
2  12.253872                                                                                
3   9.609795  9.053038                                                                      
4  11.218275  8.893657 11.363967                                                            
5   8.933017 10.775817 10.900534  6.272734                                                  
6   8.862755 11.961004 14.079231 12.244758 10.079188                                        
7   8.002884 12.009199 10.015648 14.295683 12.754016  7.623887                              
8  12.464341  8.174952 11.684419 11.243689 13.950215 13.118307 10.433698                    
9  10.230285  9.020911  9.105817 11.061137  9.250890  6.634516  8.297935  9.577125          
10  8.291082  8.067096 11.114652  8.922511 11.419875  8.497445 10.100889  9.789253  7.591539

Or using igraph library:

library(igraph)

d <- graph.data.frame(data, directed = FALSE)
dist(get.adjacency(d, attr = "y", sparse = T), diag = T)
  • Related