Problem with a loop in the adjacency matrix:
I have a dataset (all_connected_names1) of two columns: Data_Name and Name that contain names of the organizations (Data_Name) and corresponding people working in those organizations (Name). I'm trying to make an adjacency matrix based on the organizations that have same people working there - nodes are organizations from Data_Name column and edges people from Name column.
However, when I do the matrix the loop returns empty and graph shows as if there are no connections.
This is the code I'm using:
# Get unique organization names from the Data_Name column
organizations <- unique(all_connected_names1$Data_Name)
organizations
# Create an empty adjacency matrix with dimensions based on the number of organizations
adjacency_matrix <- matrix(0, nrow = length(organizations), ncol = length(organizations))
rownames(adjacency_matrix) <- as.character(organizations)
colnames(adjacency_matrix) <- as.character(organizations)
# Iterate over the rows of the combined dataset
for (i in 1:(nrow(all_connected_names1) - 1)) {
current_organization <- trimws(all_connected_names1$Data_Name[i])
next_organization <- trimws(all_connected_names1$Data_Name[i + 31])
name <- trimws(all_connected_names1$Name[i + 31])
# Check if the same name appears next to different organizations
if (is.na(current_organization) != is.na(next_organization)) {
adjacency_matrix[current_organization, next_organization] <- 1
adjacency_matrix[next_organization, current_organization] <- 1
}
# Check if the same name appears next to the same organization
if (current_organization == next_organization) {
adjacency_matrix[next_organization, next_organization] <- 1
}
}
I also tried making an adjacency list or an edge list, but with no success