R : 网络图统计每个节点在V1和V2中出现的次数

发布时间 2023-10-23 16:47:52作者: 王哲MGG_AI
# 读取txt文件
data <- read.table("your_input_file.txt", header = TRUE, stringsAsFactors = FALSE)

# 统计每个节点在V1和V2中的出现次数
V1_counts <- table(data$V1)
V2_counts <- table(data$V2)

# 获取V1和V2中所有唯一的节点名称
all_nodes <- union(names(V1_counts), names(V2_counts))

# 初始化所有节点的计数为0
total_counts <- setNames(rep(0, length(all_nodes)), all_nodes)

# 更新计数
total_counts[names(V1_counts)] <- total_counts[names(V1_counts)] + V1_counts
total_counts[names(V2_counts)] <- total_counts[names(V2_counts)] + V2_counts

# 将结果按计数从大到小排序
sorted_counts <- sort(total_counts, decreasing = TRUE)

# 将结果写入新的txt文件
output_file <- "sorted_counts.txt"
write.table(sorted_counts, output_file, quote = FALSE, row.names = TRUE, col.names = FALSE)