0

I got two large matrix with this format:

row.names  1    2     3   ...          row.names  1    2   3  ....   

A          0.1 0.2 0.3                   A        1    1   1 
B          0.4 0.9 0.3                   B        2    3   1
C          0.9 0.9 0.4                   C        1    3   1
.

And I want to obtain something like this:

X  S   CONF P
1  A   0.1  1
1  B   0.4  2
1  C   0.9  1
2  A   0.2  1
2  B  ......

Getting the colnames in one column and repeat the rownames and the information per each of the column names.

Thank you so much

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
crocket
  • 37
  • 4
  • 1
    Do you have tables or dataframes? Please provide [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas Apr 10 '14 at 09:08
  • I have large matrix, and I want the colnames to rows and the rownames repeated each time the column name changes . – crocket Apr 10 '14 at 09:37
  • Well a reproducable example would help, but they do actually say "two large matrix"... – waferthin Apr 10 '14 at 10:06
  • I know not a good title, I changed it for matrix sorry. – crocket Apr 10 '14 at 10:22

2 Answers2

2

You can do this pretty easily with some rep and c work:

out <- data.frame(X = rep(colnames(conf), each = nrow(conf)),
                  S = rep(rownames(conf), ncol(conf)),
                  CONF = c(conf), P = c(P))
out
#   X S CONF P
# 1 1 A  0.1 1
# 2 1 B  0.2 1
# 3 1 C  0.3 1
# 4 2 A  0.4 2
# 5 2 B  0.9 3
# 6 2 C  0.3 1
# 7 3 A  0.9 1
# 8 3 B  0.9 3
# 9 3 C  0.4 1

@Thomas had a similar approach (but one which matches the answer you show in your question). His answer looked like this:

cbind.data.frame(X = rep(colnames(conf), each=nrow(conf)),
                   S = rep(rownames(conf), times=nrow(conf)), 
                   CONF = matrix(t(conf), ncol=1),
                   P = matrix(t(P), ncol=1))
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • I'll delete my answer, since apparently we had the same idea. But I think it's better to `t` the matrices, so it produces the order OP requested. – Thomas Apr 10 '14 at 10:58
  • @Thomas, I thought so too, but this answer matches their "accepted" answer. If you don't mind, I'll edit in your answer here too as an alternative. Or you could just keep your answer. It'll get a vote from me at least for thinking along the same lines :-) – A5C1D2H2I1M1N2O1R2T1 Apr 10 '14 at 11:03
1

Assuming we're talking about matrices, I would convert to a data frame, add the rownames as a column and then "melt" each data.frame...

conf <- matrix(
  c(0.1, 0.4, 0.9,
    0.2, 0.9, 0.9,
    0.3, 0.3, 0.4),
  ncol=3, byrow=T
)
rownames(conf) <- c("A", "B", "C")
colnames(conf) <- 1:3

P <- matrix(
  c(1, 2, 1,
    1, 3, 3,
    1, 1, 1),
  ncol=3, byrow=T
)
rownames(P) <- c("A", "B", "C")
colnames(P) <- 1:3

library(reshape)
conf <- cbind(as.data.frame(conf), "S"=rownames(conf))
P <- cbind(as.data.frame(P), "S"=rownames(P))
out <- merge(melt(conf, id="S"), melt(P, id="S"), by=c("variable", "S"))
colnames(out) <- c("X", "S", "CONF", "P")
waferthin
  • 1,582
  • 1
  • 16
  • 27