【问题标题】:Extracting Row and column names of an adjacency matrix in R在R中提取邻接矩阵的行名和列名
【发布时间】:2015-02-26 09:13:48
【问题描述】:
    library(sna)  
    library(proxy)  
    library(igraph)  
    rawdataframe<-read.csv(file =     "~/Desktop/SNA/Techniques/LinkPrediction/Sample.csv" ,fill=TRUE,header=FALSE,sep=",")  
    rawgraphdata<-    graph.data.frame(rawdataframe,directed=TRUE,vertices=NULL)  
    adj<-get.adjacency(rawgraphdata)  
    g1<-as.matrix(adj)  
    adjmatrix<-simil(g1, method ="Jaccard", diag = TRUE, upper = TRUE ,pairwise = TRUE, by_rows = TRUE, convert_distances = TRUE,     auto_convert_data_frames = TRUE)  
    adjmatrix[adjmatrix >= 0.75]<-1   
    adjmatrix[adjmatrix <= 0.75]<-0

我使用了simil proxy measure,得到的结果如下。

输出如下:

       2 159 3 5 858 100 114 171
   2   0   0 0 0   0   0   0   0
   159 0   0 0 0   0   0   0   0
   3   0   0 0 0   0   0   0   0
   5   0   0 0 0   0   0   0   0
   858 0   0 0 0   0   0   0   0
   100 0   0 0 0   0   0   1   1
   114 0   0 0 0   0   1   0   1
   171 0   0 0 0   0   1   1   0  

我想提取值对应于 1 的(行、列)对。

【问题讨论】:

  • 试试这个which(adjmatrix==1,arr.ind=T)
  • 它返回 26 27 28。我尝试了其他参数,例如 rbind 和 cbind。但是,结果相同。

标签: r


【解决方案1】:

正如 NiceE 所指出的,您可以将whicharr.ind=1 一起使用,这将为您提供行和列索引; 收集行名和列名,例如使用apply:

> apply(which (as.matrix(adjmatrix)==1,arr.ind=T), 2, function(x)rownames(as.matrix(adjmatrix))[x])

     row   col  
[1,] "114" "100"
[2,] "171" "100"
[3,] "100" "114"
[4,] "171" "114"
[5,] "100" "171"
[6,] "114" "171"

这将适用于您的情况,因为在邻接矩阵中,行名和列名应该相同。

【讨论】:

  • arr.ind && 中的错误!is.null(d
  • 您能否发布来自dput(adjmatrix) 的内容以确保对象相似?如果我从您的问题中阅读示例矩阵,它对我来说很好。
  • dput(adjmatrix) structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), Size = 8L, Labels = c("2", "159", "3", "5", "858", "100", "114", "171"), class = c("simil", "dist"), Diag = TRUE, Upper = TRUE, method = "Jaccard", call = dist(x = x, y = y, method = method, diag = diag, upper = upper, pairwise = pairwise, by_rows = by_rows, convert_similarities = FALSE, auto_convert_data_frames = auto_convert_data_frames))
  • This is the output of the mentioned arguments
猜你喜欢
  • 1970-01-01
  • 2014-05-17
  • 2015-05-31
  • 2019-09-11
  • 2011-08-10
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多