[R] Printing vectrix

David L Carlson dc@r|@on @end|ng |rom t@mu@edu
Mon Mar 25 16:48:22 CET 2019


Here are a couple of other options. One changes the console width and the other pads the matrix before printing:

> # Print rows of 10 values
> x <- 1:25
> # Basically cheating and you don't get column numbers
> # wd depends on the size of the values and the number
> # of values
> wd <- (floor(log10(max(x)) + 2) * 10 + floor(log10(length(x))) + 3)
> options(width=wd)
> x
 [1]  1  2  3  4  5  6  7  8  9 10
[11] 11 12 13 14 15 16 17 18 19 20
[21] 21 22 23 24 25
> options(width=80)
> # Or print as a matrix
> rows <- ceiling(length(x) / 10)
> pad <- rows * 10 - length(x)
> x2 <- matrix(c(x, rep(NA, pad)), rows, 10, byrow=TRUE)
> print(x2, na.print="")
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    2    3    4    5    6    7    8    9    10
[2,]   11   12   13   14   15   16   17   18   19    20
[3,]   21   22   23   24   25                          

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of K. Elo
Sent: Monday, March 25, 2019 2:26 AM
To: r-help using r-project.org
Subject: Re: [R] Printing vectrix

Hi!

2019-03-25 kello 09:30 +0800, Steven Yen wrote:
> The second command is ugly. How can I print the 25 numbers into 2
> rows 
> of ten plus a helf row of 5? Thanks.

Something like this?

x<-1:25; for (i in seq(1,length(x),10))
print(x[i:ifelse((i+9)>length(x),length(x),i+9)])

HTH,
Kimmo

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list