r - Extract one triangle of a correlation matrix with attributes -


i have dataframe/matrix of equal rows , columns. want extract upper or lower triangle.

x<-data.frame(matrix(1:25,nrow=5)) colnames(x)<-letters[1:5] rownames(x)<-letters[1:5]  x[upper.tri(x,diag=f)] 

from result, not possible combination of column , row value came from. so, have row , column attributes in results. this:

col row val b     6 c     11 c   b   12 ... 

i need large correlation matrix. thanks.

first, make things unambiguous, change

colnames(x) <- letters[6:10] 

use expand.grid row , column names this

rowcol <- expand.grid(rownames(x), colnames(x)) 

to correct rows data frame, take

labs <- rowcol[as.vector(upper.tri(x,diag=f)),] df <- cbind(labs, x[upper.tri(x,diag=f)]) colnames(df) <- c("row","col","val") df[,c(2,1,3)] ##    col row val ## 6    g     6 ## 11   h    11 ## ... 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -