制作邻接矩阵时出现循环问题

Problem with a loop when making adjacency matrix

提问人:Andjela Perovic 提问时间:6/2/2023 最后编辑:marc_sAndjela Perovic 更新时间:6/3/2023 访问量:16

问:

邻接矩阵中的循环问题:

我有一个包含两列的数据集(all_connected_names1):其中包含组织名称()和在这些组织中工作的相应人员()。我正在尝试根据有相同人员在那里工作的组织制作一个邻接矩阵 - 节点是来自列的组织,边缘是来自列的人。Data_NameNameData_NameNameData_NameName

但是,当我执行矩阵时,循环返回空,并且图形显示好像没有连接。

这是我正在使用的代码:

# 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
  }
}

我还尝试制作邻接列表或边缘列表,但没有成功

循环 社交网络 邻接矩阵 列表

评论


答: 暂无答案