K. Taraka Rama
2013-Sep-18 13:18 UTC
[R] Converting a asymmetric data frame to symmetric matrix
Hi, I have a pair-wise distance vector. FOr objects: a,b,c, it is: (a,b) :5, (b,c) :6, (a,c) : 7. I want to convert it into a symmetric matrix. I used cast function but the function does not fill the matrix like a triangular matrix. How do I get a symmetric matrix? -- --Taraka
Hi, It is not clear whether it is a data frame or vector.? Please use ?dput() to show the example dataset. vec1<- 5:7 mat1<- matrix(0,3,3,dimnames=list(letters[1:3],letters[1:3])) mat2<- mat1 ?nm1<- c("ab","bc","ac") ?vec2<-paste0(colnames(mat1)[col(mat1)],rownames(mat1)[row(mat1)]) ?mat1[match(nm1,vec2)]<- vec1 ?mat1 #? a b c #a 0 0 0 #b 5 0 0 #c 7 6 0 #or ?vec2<- c(5,7,6) ?mat2[upper.tri(mat2)]<- vec2 ?mat2[lower.tri(mat2)]<- vec2 ?mat2 #? a b c #a 0 5 7 #b 5 0 6 #c 7 6 0 A.K. ----- Original Message ----- From: K. Taraka Rama <taraka at fripost.org> To: r-help at r-project.org Cc: Sent: Wednesday, September 18, 2013 9:18 AM Subject: [R] Converting a asymmetric data frame to symmetric matrix Hi, I have a pair-wise distance vector. FOr objects: a,b,c, it is: (a,b) :5, (b,c) :6, (a,c) : 7. I want to convert it into a symmetric matrix. I used cast function but the function does not fill the matrix like a triangular matrix. How do I get a symmetric matrix? -- --Taraka ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.