对两个以上的列使用igraph

对两个以上的列使用igraph,r,networking,tree,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

我有一个数据帧(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)
库(igraph)

df我们可以通过在
列表中成对地对列进行子集,使用
rbindlist
(从
data.table
)绑定数据集,转换为
graph
data.frame和
plot

library(data.table)
library(igraph)
plot(graph.data.frame(rbindlist(lapply(seq(ncol(df)-1), function(i) df[i:(i+1)]))))