# 读取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)
标签:次数,网络图,V1,V2,data,节点 From: https://www.cnblogs.com/wzbzk/p/17782854.html