이것은 최소한 시작해야합니다. 나는 adjacency matrix
를 얻기 위해 생각할 수있는 가장 간단한 방법은 reshape
이하는 것입니다 다음 다음과 같이 igraph
를 사용하여 그래프 구축 :
여기
# load data
df <- read.table(header=T, stringsAsFactors=F, text=" V1 V2 V3
164885 431072 3
164885 164885 24
431072 431072 5")
> df
# V1 V2 V3
# 1 164885 431072 3
# 2 164885 164885 24
# 3 431072 431072 5
# using reshape2's dcast to reshape the matrix and set row.names accordingly
require(reshape2)
m <- as.matrix(dcast(df, V1 ~ V2, value.var = "V3", fill=0))[,2:3]
row.names(m) <- colnames(m)
> m
# 164885 431072
# 164885 24 3
# 431072 0 5
# load igraph and construct graph
require(igraph)
g <- graph.adjacency(m, mode="directed", weighted=TRUE, diag=TRUE)
> E(g)$weight # simple check
# [1] 24 3 5
# get adjacency
get.adjacency(g)
# 2 x 2 sparse Matrix of class "dgCMatrix"
# 164885 431072
# 164885 1 1
# 431072 . 1
# get shortest paths from a vertex to all other vertices
shortest.paths(g, mode="out") # check out mode = "all" and "in"
# 164885 431072
# 164885 0 3
# 431072 Inf 0
지금까지 시도한 것은 무엇입니까? 나는 당신이 여기에 새로운 것을 보았을 것입니다. 아마도 스택 오버플로가 시도 할 때 프로그래밍 질문에 대한 답변을 얻을 수있는 곳이라는 것을 알지는 못하지만 ** 붙어 있습니다 **. 이 시점에서 당신이 문제를 해결하려고 시도한 것은 분명하지 않습니다. 나는 [Stack Overflow FAQ] (http://stackoverflow.com/faq)가 시작하기에 좋은 곳이라고 생각한다. R에 대한 질문이 있으면 [이 게시물] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)은 반드시 읽어야합니다. – SlowLearner