我想通过使用R。
将加权边缘列表与相同边缘(不考虑方向)组合在一起例如:
temp=rbind(c('A','B'),
c('B','A'))
colnames(temp)=c('from','to')
weight=rep(1,2)
cbind(temp,weight)
from to weight
[1,] "A" "B" "1"
[2,] "B" "A" "1"
最终结果喜欢这个:
from to weight
[1,] "A" "B" "2"
任何想法?这个问题困扰了我很久!! 谢谢。
答案 0 :(得分:0)
我建议您尝试将temp
转换为二进制matrix
######Data Input####
temp=rbind(c('A','B'),
c('B','A'),c('C','D'))
colnames(temp)=c('from','to')
weight=rep(1,3)
####################
df=cbind(temp,weight)
x=table(row(temp), temp)
A=apply(x,1,paste,collapse=" ")
df=cbind(df,A)
> df
from to weight A
1 "A" "B" "1" "1 1 0 0"
2 "B" "A" "1" "1 1 0 0"
3 "C" "D" "1" "0 0 1 1"
使用新的df
列A
,您可以轻松获得所需的内容,例如:dplyr::group_by
library(dplyr)
df%>%group_by(A)%>%dplyr::mutate(weight=n())%>%filter(row_number()==1)
from to weight A
<fctr> <fctr> <int> <fctr>
1 A B 2 1 1 0 0
2 C D 1 0 0 1 1