あなたはこのようなあなたの行列の最初の列を削除したいと思います:
Matrix <- as.matrix(Matrix_One)[,-1]
あなたの隣接行列の値が数値であれば、取得する代わりにas.matrix()
のdata.matrix()
を使用することをお勧めかもしれませんあなたのマトリックスの文字列ではなく数値です。隣接行列の値は、しばしば数値として与えられる各エッジ重みに対応する重みである。 Rは、この検討し、使用可能な隣接行列としてデータを読むために取得するには
:
# Assuming your csv file is like this...
csv <- "X,A,B,C,B,E
A,0,0,1,0,1
B,1,0,0,0,0
C,1,1,0,0,0
D,1,0,0,0,0
E,0,0,0,0,0"
# ... with first row and column indicating node name in your network.
# To keep names, we could keep the header and use it as a list of nodes:
Matrix_One <- read.csv2("Network Matrix.csv", sep=",", header=TRUE)
Nodelist <- names(Matrix_One)[-1]
# The matrix should include the first row (which is data),
# but not the first column (which too contains node-names) of the df:
Matrix <- data.matrix(Matrix_One)[,-1]
# As the matrix is now of the size N by N, row- and colnames makes for a neat matrix:
rownames(Matrix) <- colnames(Matrix) <- Nodelist
# Look at it
Matrix
# Use igraph to make a graph-object and visualize it
library(igraph)
g <- graph_from_adjacency_matrix(Matrix, mode="directed", weighted=NULL)
plot(g)
パッケージ時代遅れ(と私は信じてCRANから削除)されます。上記の例では、代わりにigrpah
を使用しています。これは、包括的なネットワークデータ管理パッケージで、優れた視覚化機能を備えています。
あなたはに固執することを選択した場合、あなたはfirst_network <- graph.adjacency(Matrix, mode= "directed", weighted=NULL)
のような四角いすぎMatrix
を取る:上記のコードの結果は次のようなものになります。
あなたのcsvファイルの最初の3行と 'head(Matrix)'と 'dim(Matrix)'を表示してください – G5W
残念ですが、最初の行はヘッダーとして読み込まれますが、最初の列は読み込まれません。 –
"非正方行列"とは、その幅と異なる高さを持つ行列を指します。 'nrow(Matrix)'幅 'ncol(Matrix)'を比較して、最初の列(おそらく垂直ヘッダを含んでいる)があなたの行列を背の高さよりも広げている様子を見てください。 – nJGL