[R] Printing vector

Steven @yen @end|ng |rom hqu@edu@cn
Sat Jul 20 22:34:22 CEST 2019


Dear All:

Below is what I meant. Procedure print0 allows me to print a vector of 
length 53 in four rows of 10 plus 1 row of 3 (Ido not like the NA). This 
is silly. I am hoping that there is a candid way to print the matrix. 
Thank you.

Steven Yen

===
n<-53; x<-runif(n); # x<-round(x,2)

print0<-function(x,c=10,digits=2){
# ******************************************
# Print vector in rows of a specified length
# ******************************************
   n<-length(x)
   r<-n/c; if(n%%c>0) r<-as.integer(r)+1
   y<-rep(NA,r*c)
   y[1:n]<-x
   y<-matrix(y,r,c,byrow=T)
   y<-round(y,digits=digits)
   y
}

print0(x,c=10,digits=3)

# result
# [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
# [1,] 0.576 0.291 0.600 0.515 0.135 0.335 0.296 0.911 0.454 0.696
# [2,] 0.699 0.728 0.442 0.469 0.996 0.539 0.772 0.768 0.652 0.882
# [3,] 0.614 0.228 0.748 0.071 0.788 0.428 0.885 0.722 0.432 0.881
# [4,] 0.422 0.148 0.459 0.870 0.044 0.421 0.282 0.337 0.751 0.579
# [5,] 0.468 0.659 0.446 0.199 0.388 0.576 0.829 0.186 0.823 0.960
# [6,] 0.880 0.944 0.709    NA    NA    NA    NA    NA NA    NA

Steven 於 2019/7/20 下午 02:00 寫道:
>
> Is there a convenient way to print a vector into rows of a specified 
> column length? What I need is to print in the old FORTRAN format, viz.,
>
> format(10F8.2)
>
> which would print, for instance, a vector of 25 into two rows of 10 
> plus an incomplete row of 5. I managed to write a procedure for that 
> task, as shown below (except that I prefer simply blanks rather than 
> the NA). I am too embarrassed to even show the procedure. In short, I 
> like to print in the above FORTRAN format. Thank you.
>
> ----
>
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.66 0.26 0.82 
> 0.73 0.13 0.05 0.56 0.67 0.74 0.87 [2,] 0.91 0.25 0.40 0.39 0.50 0.89 
> 0.07 0.84 0.14 0.75 [3,] 0.38 0.08 0.86 0.97 0.56 NA NA NA NA NA

	[[alternative HTML version deleted]]



More information about the R-help mailing list