1

I have two matrices

mat1 <- matrix(1:4, nrow=2)
dimnames(mat1) <- list(letters[1:2], letters[3:4])
mat2 <- matrix(11:19, nrow=3)
dimnames(mat2) <- list(letters[10:12], letters[13:15])

I want to "cbind" the two matrices into an object x so I can write.table(x, "clipboard", sep="\t", col.names=NA), paste into Excel, and have the two matrices show up side by side with their dimnames intact.

    c   d       m   n   o
a   1   3   j   11  14  17

b   2   4   k   12  15  18

            l   13  16  19

What's the best way to do this?

IRTFM
  • 258,963
  • 21
  • 364
  • 487
kevinykuo
  • 4,600
  • 5
  • 23
  • 31
  • Can you show the desired output for the combination of the matrices, which have different dimensions? – Sven Hohenstein Dec 03 '13 at 20:15
  • found the solution at [http://stackoverflow.com/questions/7962267/cbind-a-df-with-an-empty-df-cbind-fill](http://stackoverflow.com/questions/7962267/cbind-a-df-with-an-empty-df-cbind-fill). that was embarrassing; how do I mark my question as dupe? – kevinykuo Dec 03 '13 at 21:04
  • Wait a minute. Does that preserve row names? – IRTFM Dec 03 '13 at 21:07
  • 1
    I suppose you could mark as a duplicate by voting to close but I don't think the rownames feature is a trivial aspect, nor is the output of a row-oriented stream to the Clipboard. – IRTFM Dec 03 '13 at 21:20
  • yeah just kidding, I thought my "j k l" showed up but it didn't :P – kevinykuo Dec 03 '13 at 21:33
  • Well, I think my answer is pretty kewl, and even if I get no rep for it, it will still save me time in the future. I do a lot of copying matrices to Excel from the console and then using the fixed format feature of text-to-columns off the Data menu. Thanks for the interesting question. – IRTFM Dec 04 '13 at 02:14

1 Answers1

1

Not possible at least with typical base R functions. You would need to augment the smaller (or possible both) so that they can be cbind()-ed and then pasted (with tab separators) into Excel. Even getting the operation of copying a stream of characters into the Clipboard that would carry out this operation on one matrix is not trivial. The Ripley-Venables authored MASS package has a write.matrix function, but it does not write rownames, only column names. This is derivative (omitting the blocking option) function that first adds a blank in the upper left corner and cbinds the rownames before processing. It could be paired with a function that inserted the rownames in the second. Perhaps the cbind.fill function implemented with

matbound <-cbind.fill(mat1, cbind(rownames(mat2),mat2) )

Followed by this hack of the second portion of the MASS::write.matrix:

write.matrix.clip <-
function (x, file = "", sep = "\t") 
{clip <- pipe("pbcopy", "w")
    x <- as.matrix(x)
    p <- ncol(x)
    cn <- c(" ", colnames(x))
    cat(c(cn, format(t( cbind(rownames(x),x)))), file = clip, sep = c(rep(sep, 
        p ), "\n")); close(clip)
}
write.matrix.clip(matbound)

(The clipboard value could be "clipboard" on a windows machine.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • `write.table(matbound, "clipboard", sep="\t", col.names=NA, na="")` works for me, without having to play around with `write.matrix`. Did I just get lucky in this case? – kevinykuo Dec 04 '13 at 03:09
  • I think you will find that the column names are not aligned properly with write.table on a matrix object. .... Oh, you solved that by not putting column names in. So you did or you didn't want column names? – IRTFM Dec 04 '13 at 04:26
  • I do want columns names. "By default there is no column name for a column of row names. If col.names = NA and row.names = TRUE a blank column name is added, which is the convention used for CSV files to be read by spreadsheets" http://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html – kevinykuo Dec 04 '13 at 04:31
  • I was using the console output and do find that that works for clipboard despite not printing tabs to the console. – IRTFM Dec 04 '13 at 04:33