Skip to content

Printing vectrix

3 messages · Steven Yen, Kimmo Elo, David L Carlson

#
I like to print a vector, wrapped by rows of 10.
Below the first command below works for 20 numbers.

The second command is ugly. How can I print the 25 numbers into 2 rows 
of ten plus a helf row of 5? Thanks.

 > x<-1:20; matrix(x,nrow=2,byrow=T)
 ???? [,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
 > x<-1:25; matrix(x,nrow=2,byrow=T)
 ???? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
[1,]??? 1??? 2??? 3??? 4??? 5??? 6??? 7??? 8??? 9??? 10??? 11 12??? 13
[2,]?? 14?? 15?? 16?? 17?? 18?? 19?? 20?? 21?? 22??? 23??? 24 25???? 1
Warning message:
In matrix(x, nrow = 2, byrow = T) :
 ? data length [25] is not a sub-multiple or multiple of the number of 
rows [2]
 >
#
Hi!
2019-03-25 kello 09:30 +0800, Steven Yen wrote:
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
#
Here are a couple of other options. One changes the console width and the other pads the matrix before printing:
[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
[,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 at r-project.org> On Behalf Of K. Elo
Sent: Monday, March 25, 2019 2:26 AM
To: r-help at r-project.org
Subject: Re: [R] Printing vectrix

Hi!
2019-03-25 kello 09:30 +0800, Steven Yen wrote:
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 at 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.