1

I am writing a function that extracts information from models, and I am wanting to separate parts of the output, like the mtable in the memisc package, a row of hyphens or dashes between one block of output like parameter estimates and another block like information criteria or separating different models. so if I have a two matrices and I want to separate them by a dashed line like

so if i have two matrices combined they would be

structure(c(1, 3, 5, 7, 2, 4, 6, 8), .Dim = c(4L, 2L))

but separate they would be

structure(c(1, 3, 2, 4), .Dim = c(2L, 2L))
structure(c(1, 3, 2, 4), .Dim = c(2L, 2L))

and wanting them to show up as matrices but how do i add a row of characters to it instead of just doing it manually and guessing on how many to put there without the quotes or the line numbers?

I have tried paste command, format, cat, rep("=") and I know that will kind of work but don't know how to control the width. I know I am kind of vague and apologize for that.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
JPK
  • 39
  • 6

1 Answers1

0

rowSums(nchar(x)) + ncol(x) should give you a good enough approximation to figure out how wide your row of = should be.

An example:

x <- matrix(c(10, 2, 123, 4), ncol = 2)
rowSums(nchar(x)) + ncol(x) - 1
# [1] 6 3
max(rowSums(nchar(x)) + ncol(x) - 1)
# [1] 6

For figuring this out across multiple matrices, I suppose you could calculate the width across multiple matrices with something like this:

max(vapply(list(x, y), function(i) {
  max(rowSums(nchar(i)) + ncol(i))
}, 1))

where list(x, y) is a list of a matrix named "x" and one named "y".

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • thanks, this along with finding another post helped me, found a similar post on the side. – JPK Dec 18 '13 at 04:37
  • @JPK, cool. You can also look at the code of the functions you refer to and figure out how they calculated the width. The `by` function in base R, for instance, uses the `width` "option" as a basis for how many dashes it should print. – A5C1D2H2I1M1N2O1R2T1 Dec 18 '13 at 04:39
  • i know how to look at code usually but i couldnt find all of it but did find some more of it by looking at a related post (http://stackoverflow.com/questions/15046682/creating-nice-looking-output?rq=1) sorry for the superfluous question here, i didnt find that response until i asked it. – JPK Dec 18 '13 at 10:23