Here is a dataframe in R
:
d <- data.frame(c = 1:3, c2 = 4:6, c3 = 7:9)
rownames(d) <- c('r1', 'r2', 'r3')
d
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
I want to assemble a new dataframe based on all the elements (excluding the diagonals) in d
as below:
row col value
r1 c2 4
r1 c3 7
r2 c1 2
r2 c3 8
r3 c1 3
r3 c2 6
The order of the rows does not matter. What is an efficient way to achieve this? The question has an extra part from the typical reshaping since I want to exclude those diagonal elements.