I am trying to build an adjacency matrix for a web network to determine the most popular website through a pagerank algorithm.
In order to run the algorithm I need to create an adjacency matrix from a .txt file that contains the links from page to page, which I did using the igraph package as follows:
A_h = graph_from_data_frame(hollins)
A_h = as_adjacency_matrix(A_h)
To be able to run the algorithm, I need to first make sure no page has all 0's by linking the page to itself. To do this, I used this for loop:
for(i in 1:nrow(A_h)){
if(rowSums(A_h) == 0){
A_h[i,i] = 1}
Then I need to normalize the rows by making sure the entries of the rows add up to 1:
norm_A_h = A_h/rowSums(A_h)
I tried doing this on a dummy, simple matrix and it worked well:
T = matrix(c(0, 3, 4, 0, 8, 4, 0, 4, 5), nrow = 3, ncol = 3)
for(i in 1:nrow(T)){
if(rowSums(T) == 0){
T[i,i] = 1}
}
which(rowSums(A_h)==0)
T = T/rowSums(T)
However, when I try to run it on the sparse matrix, it looks like I still end up with all 0 rows after checking it:
which(rowSums(A_h)==0)
Any ideas as to why it doesn't work with the sparse matrix?