r - 对两列以上使用 igraph

标签 r networking tree igraph

我有一个数据框(df)如下。

V1 V2 V3 V4
 A B  C  D
 A G  R  T
 M A  R  H

我的要求是绘制从 V1 到 V2 到 V3 到 V4 的转换图。当我使用 igraph 命令时

g = graph.data.frame(df)

我看到 V3 和 V4 列被删除了。有没有办法构造这样的转换图。

最佳答案

试试这个:

library(igraph)
df <- as.matrix(df)
# to transform the df to the format that igraph expects, following will suffice for your example 
df <- as.data.frame(rbind(df[,1:2], df[,2:3], df[,3:4]))
# if you want to make it generic try the following instead
# df <- as.data.frame(do.call(rbind, lapply(1:(ncol(df)-1), function(i) df[,i:(i+1)])))
g = graph.data.frame(df)
plot(g)

enter image description here

关于r - 对两列以上使用 igraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478736/

相关文章:

mysql - ubuntu中如何访问本地网络其他系统的mysql数据库连接

algorithm - 迭代具有 O(1) 辅助空间的二叉树

perl - 如何在 Perl 哈希中表示文件系统的符号链接(symbolic link)?

r - if 函数出错

r - 转换 data.table 中的*一些*列类

javascript - R网络D3包: forceNetwork() coloring

iphone - 需要 Bonjour 两次?

r - 循环内部与外部功能的速度差异

c++ - 使用 C/C++ Socket 模拟网络条件

tree - 在 graphviz/点树可视化中强制左右节点顺序