Reputation: 159
I have got a matrix describing relationship between diffetent people. If there is any connection between people I have "1" in a particular cell, otherwise - "0". How to turn this into a data.frame with two columns looks like:
person1 -- person4
person1 -- person6
person2 -- person1
?
Upvotes: 0
Views: 137
Reputation: 52697
Use melt
from reshape2
:
library(reshape2)
set.seed(1)
mx <- matrix(sample(0:1, 9, r=T), nrow=3, dimnames=replicate(2, paste0("p", 1:3), s=F))
# p1 p2 p3
# p1 0 1 1
# p2 0 0 1
# p3 1 1 1
melt(mx)
# Var1 Var2 value
# 1 p1 p1 0
# 2 p2 p1 0
# 3 p3 p1 1
# 4 p1 p2 1
# 5 p2 p2 0
# 6 p3 p2 1
# 7 p1 p3 1
# 8 p2 p3 1
# 9 p3 p3 1
Upvotes: 1