melt the lower half from systematic matrix in R -
given have 3 three systematic matrix.
> x<-matrix(1:9,3) > x[lower.tri(x)] = t(x)[lower.tri(x)] > x [,1] [,2] [,3] [1,] 1 4 7 [2,] 4 5 8 [3,] 7 8 9 then apply library reshape2 make in long-format.
> library(reshape2) > x <- melt(x) > x var1 var2 value 1 1 1 1 2 2 1 4 3 3 1 7 4 1 2 4 5 2 2 5 6 3 2 8 7 1 3 7 8 2 3 8 9 3 3 9 as upper diagonal , bottom diagonal identical, need half of result, below.
var1 var2 value 1 1 1 2 1 4 3 1 7 2 2 5 3 2 8 3 3 9 any elegant approach this?
you can change values bottom or upper half na, , melt ignoring missing values, assume there not missing values in matrix or don't need keep them in result if there are:
x[upper.tri(x)] = na reshape2::melt(x, na.rm=t) # var1 var2 value #1 1 1 1 #2 2 1 4 #3 3 1 7 #5 2 2 5 #6 3 2 8 #9 3 3 9
Comments
Post a Comment