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]
>
syen at hqu.edu.cn
[[alternative HTML version deleted]]
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
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 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:
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 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.