one way is the following:

mat <- matrix(1:12, 3, 4, TRUE)
dimnames(mat) <- list(c("A", "E", "F"), LETTERS[1:4])

cnams <- colnames(mat)
rnams <- rownames(mat)
unq.nams <- unique(c(cnams, rnams))
out <- matrix(0, length(unq.nams), length(unq.nams),
    dimnames = list(unq.nams, unq.nams))
p1 <- paste(cnams[col(mat)], rnams[row(mat)], sep = "\\")
p2 <- paste(unq.nams[col(out)], unq.nams[row(out)], sep = "\\")
out[match(p1, p2)] <- mat
out <- out + t(out)
diag(out) <- diag(out)/2
out


I hope it helps.

Best,
Dimitris


Nathan S. Watson-Haigh wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have a rectangular matrix of size 920 by 85. I'd like to coerce it into a
square matrix such that all row/col names are present in the new matrix and the
additional values are zero.

As an example:

   A  B  C  D
A  1  2  3  4
E  5  6  7  8
F  9 10 11 12

Would be coerced to:
   A  B  C  D  E  F
A  1  2  3  4  5  9
B  2  0  0  0  6 10
C  3  0  0  0  7 11
D  4  0  0  0  8 12
E  5  6  7  8  0  0
F  9 10 11 12  0  0

Any thoughts on how to achieve this?

Cheers,
Nathan


- --
- --------------------------------------------------------
Dr. Nathan S. Watson-Haigh
OCE Post Doctoral Fellow
CSIRO Livestock Industries
Queensland Bioscience Precinct
St Lucia, QLD 4067
Australia

Tel: +61 (0)7 3214 2922
Fax: +61 (0)7 3214 2900
Web: http://www.csiro.au/people/Nathan.Watson-Haigh.html
- --------------------------------------------------------

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAko4fvEACgkQ9gTv6QYzVL5gywCgsOpHJpTUXW98oHJ7jsSlo47X
mToAoI08QcZHvOgSFteqmQ/acAwDkaiR
=Hyod
-----END PGP SIGNATURE-----

______________________________________________
R-help@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.


--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
R-help@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.

Reply via email to