0

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

?

group413
  • 159
  • 1
  • 1
  • 5
  • possible duplicate of [melt the lower half matrix in R](http://stackoverflow.com/questions/8221630/melt-the-lower-half-matrix-in-r) – bright-star Apr 22 '14 at 20:28

1 Answers1

1

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
BrodieG
  • 51,669
  • 9
  • 93
  • 146