如何根据原始数据计算关系以进行网络分析
How to calculate ties for network analysis from raw data
我有一些关于已发表论文的数据,如下所示:
paper <- c("paper1", "paper1", "paper2", "paper3", "paper3", "paper4", "paper4", "paper5")
author <- c("author1", "author2", "author1", "author2", "author1", "author2", "author3", "author2")
df1 <- data.frame(paper, author)
如何使用这种格式进行运行网络分析?
from <- c("a1", "a2", "a2")
to <- c("a2", "a3", "a3")
weight <- c(2,0,1)
df2 <- data.frame(from, to, weight)
我曾尝试干预 pivot_wider()
和 widyr::pairwise_count
,但尚未产生所需的输出。
这是一个基本的 R 选项 -
用 combn
创建成对组合,然后用 tapply
计算有多少 paper
有这个组合
result <- do.call(rbind, combn(unique(df1$author), 2, function(x) {
data.frame(from = x[1], to = x[2],
weight = sum(tapply(df1$author, df1$paper, function(y) all(x %in% y))))
}, simplify = FALSE))
result
# from to weight
#1 author1 author2 2
#2 author1 author3 0
#3 author2 author3 1
我有一些关于已发表论文的数据,如下所示:
paper <- c("paper1", "paper1", "paper2", "paper3", "paper3", "paper4", "paper4", "paper5")
author <- c("author1", "author2", "author1", "author2", "author1", "author2", "author3", "author2")
df1 <- data.frame(paper, author)
如何使用这种格式进行运行网络分析?
from <- c("a1", "a2", "a2")
to <- c("a2", "a3", "a3")
weight <- c(2,0,1)
df2 <- data.frame(from, to, weight)
我曾尝试干预 pivot_wider()
和 widyr::pairwise_count
,但尚未产生所需的输出。
这是一个基本的 R 选项 -
用 combn
创建成对组合,然后用 tapply
计算有多少 paper
有这个组合
result <- do.call(rbind, combn(unique(df1$author), 2, function(x) {
data.frame(from = x[1], to = x[2],
weight = sum(tapply(df1$author, df1$paper, function(y) all(x %in% y))))
}, simplify = FALSE))
result
# from to weight
#1 author1 author2 2
#2 author1 author3 0
#3 author2 author3 1