在R中使用graph.adjacency() [英] using graph.adjacency() in R

查看:1014
本文介绍了在R中使用graph.adjacency()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(igraph)
rm(list = ls) ())
dat = read.csv(file.choose(),header = TRUE,row.names = 1,check.names = T)#读取.csv文件
m = as.matrix(dat )
net = graph.adjacency(adjmatrix = m,mode =undirected,weighted = TRUE,diag = FALSE)

其中我使用csv文件作为输入,其中包含以下数据:

  23732 23778 23824 23871 58009 58098 58256 
23732 0 8 0 1 0 10 0
23778 8 0 1 15 0 1 0
23824 0 1 0 0 0 0 0
23871 1 15 0 0 1 5 0
58009 0 0 0 1 0 7 0
58098 10 1 0 5 7 0 1
58256 0 0 0 0 0 1 0

在此之后,我使用以下命令检查重量值:

  E (净值)$重量

预期输出有点像这样:

 > E(净值)$重量
[1] 8 1 10 1 15 1 1 5 7 1



<但是我得到了奇怪的值(每次都不一样):

 > E(净值)$重量
[1] 2.121996e-314 2.121996e-313 1.697597e-313 1.291034e-57 1.273197e-312 5.092790e-313 2.121996e-314 2.121996e-314 6.320627e-316 2.121996 e-314 1.273197e-312 2.121996e-313
[13] 8.026755e-316 9.734900e-72 1.273197e-312 8.027076e-316 6.320491e-316 8.190221e-316 5.092790e-313 1.968065e-62 6.358638e-316

我无法找到我在做什么错在哪里?
请帮助我获得正确的预期结果,并且请告诉我为什么会出现这种奇怪的输出,并且每次运行它时都会有所不同。



感谢,
Nitin

解决方案

这个问题似乎是由于矩阵元素的数据类型。 graph.adjacency 需要类型 numeric 的元素。不知道它是否有bug。



完成后,

  m < -  as.matrix(dat)

将其模式设置为 numeric by:

  mode(m)<  - numeric

然后做:

  net < -  graph.adjacency(m,mode =undirected,weighted = TRUE,diag = FALSE)
> E(净值)$重量
[1] 8 1 10 1 15 1 1 5 7 1


I have a sample code in R as follows:

library(igraph)
rm(list=ls())
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=T) # read .csv file
m=as.matrix(dat)
net=graph.adjacency(adjmatrix=m,mode="undirected",weighted=TRUE,diag=FALSE)

where I used csv file as input which contain following data:

    23732   23778   23824   23871   58009   58098   58256
23732   0   8   0   1   0   10  0
23778   8   0   1   15  0   1   0
23824   0   1   0   0   0   0   0
23871   1   15  0   0   1   5   0
58009   0   0   0   1   0   7   0
58098   10  1   0   5   7   0   1
58256   0   0   0   0   0   1   0

After this I used following command to check weight values:

E(net)$weight

Expected output is somewhat like this:

> E(net)$weight
 [1]  8  1 10  1 15  1  1  5  7  1

But I'm getting weird values (and every time different):

> E(net)$weight
 [1] 2.121996e-314 2.121996e-313 1.697597e-313  1.291034e-57 1.273197e-312 5.092790e-313 2.121996e-314 2.121996e-314 6.320627e-316 2.121996e-314 1.273197e-312 2.121996e-313
[13] 8.026755e-316  9.734900e-72 1.273197e-312 8.027076e-316 6.320491e-316 8.190221e-316 5.092790e-313  1.968065e-62 6.358638e-316

I'm unable to find where and what I am doing wrong? Please help me to get the correct expected result and also please tell me why is this weird output and that too every time different when I run it.??

Thanks, Nitin

解决方案

The problem seems to be due to the data-type of the matrix elements. graph.adjacency expects elements of type numeric. Not sure if its a bug.

After you do,

m <- as.matrix(dat)

set its mode to numeric by:

mode(m) <- "numeric"

And then do:

net <- graph.adjacency(m, mode = "undirected", weighted = TRUE, diag = FALSE)
> E(net)$weight
[1]  8  1 10  1 15  1  1  5  7  1

这篇关于在R中使用graph.adjacency()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆