我想通过匹配表中的列值来创建边缘,所以基本上是:
V1 V2 V3
A 1 1 0
B 1 0 1
C 0 1 1
D 1 0 1
如果我有一张这样的表,那么我想制作一个边缘列表
A - B
A - D
A - C
B - C
B - D
因此,每当列值与每行匹配时,我都想创建一个边。我查看了很多文档,但我似乎无法找出与此类似的东西。任何帮助将不胜感激!
答案 0 :(得分:2)
在您的数据上使用crossprod
后,我会尝试使用“igraph”包。假设您的data.frame
被称为“mydf”:
out <- crossprod(t(mydf))
out[lower.tri(out, diag=TRUE)] <- 0
library(igraph)
g <- graph.adjacency(out)
get.edgelist(g)
# [,1] [,2]
# [1,] "A" "B"
# [2,] "A" "C"
# [3,] "A" "D"
# [4,] "B" "C"
# [5,] "B" "D"
# [6,] "B" "D"
# [7,] "C" "D"
如果您不想复制,可以使用:
g <- graph.adjacency(out > 0)
get.edgelist(g)
答案 1 :(得分:1)
试试这个:
#dummy data
df <- read.table(text="
A 1 1 0
B 1 0 1
C 0 1 1
D 1 0 1",sep=" ", as.is=TRUE)
#get names where 1 per column
lst <- sapply(2:ncol(df), function(j)
df[df[,j]==1,1])
#make combinations
lst_comb <- sapply(1:(ncol(df)-1), function(i)
combn(lst[[i]],2))
#output
matrix(sort(unique(
unlist(
sapply(1:length(lst_comb),function(i){
x <- t(lst_comb[[i]])
paste0(x[,1],"-",x[,2])
})))))
# [,1]
#[1,] "A-B"
#[2,] "A-C"
#[3,] "A-D"
#[4,] "B-C"
#[5,] "B-D"
#[6,] "C-D"
答案 2 :(得分:1)
以下是基于combn
的方法:
sort(unique(unlist(apply(df, 2, function(x)combn(rownames(df)[which(x==1)], 2, FUN=paste, collapse=" - ")))))
whith df
作为数据,为您提供:
[1] "A - B" "A - C" "A - D" "B - C" "B - D" "C - D"
答案 3 :(得分:1)
dat<- read.table(text=" ID V1 V2 V3
A 1 1 0
B 1 0 1
C 0 1 1
D 1 0 1", header= TRUE)
library(reshape2)
library(tnet)
dat2 <- melt(dat, id= "ID")
dat2 <- dat2[dat2$value > 0 ,]
dat3 <- as.tnet(cbind(dat2[,1],dat2[,2]), type="binary two-mode tnet")
dat3 <- projecting_tm(dat3, method = "sum")[1:2]
dat3[dat3 == 1] <- "A" # there is an easier way to change names
dat3[dat3 == 2] <- "B"
dat3[dat3 == 3] <- "C"
dat3[dat3 == 4] <- "D"
dat3[!duplicated(t(apply(dat3, 1, sort))), ]
# i j
#1 A B
#2 A C
#3 A D
#5 B C
#6 B D
#9 C D