[R] Printing vector

Steven @yen @end|ng |rom hqu@edu@cn
Mon Jul 22 20:07:15 CEST 2019


Thank you, Gentlemen. That serves my need. Bill's routine is great.

Also, Rui: Is there a way to get rid of the filled "NA" and use space 
instead. Using fill = "" does not help either; it causes all numbers to 
be embraced with quotations. Finally, I have no idea why Rui's message 
did not reach me at all (not even in the junk mail box). But, obviously 
Bill had received it. Hmm.

Steven Yen

William Dunlap 於 2019/7/23 上午 12:33 寫道:
> The following mimics Fortran printing with format 
> <perLine>F<fWidth>.<fPrecision>.
>
> print1 <- function (x, perLine = 10, fWidth = 8, fPrecision = 2, 
> fortranStars = TRUE)
> {
>     format <- paste0("%", fWidth, ".", fPrecision, "f")
>     oldWidth <- getOption("width")
>     on.exit(options(width = oldWidth))
>     options(width = perLine * fWidth)
>     fx <- sprintf(format, x)
>     if (fortranStars) {
>         fx[nchar(fx) > fWidth] <- strrep("*", fWidth)
>     }
>     cat(fx, sep = "", fill = TRUE)
>     invisible(x)
> }
>
> Compare
> > print1((-1.7)^(1:24))
>    -1.70    2.89   -4.91    8.35  -14.20   24.14  -41.03 69.76 -118.59 
>  201.60
>  -342.72  582.62 -990.46 1683.78-2862.42 
> 4866.12-8272.4014063.08********40642.31
> ********************************
> with the output from the Fortran
> % cat a.f
>       double precision x(24);
>       integer i
>       do 10 i=1,24
>         x(i) = (-1.7d0) ** i
>  10   continue
>       write(6, "(10f8.2)") x
>       stop
>       end
> % gfortran a.f
> % ./a.out
>    -1.70    2.89   -4.91    8.35  -14.20   24.14  -41.03 69.76 -118.59 
>  201.60
>  -342.72  582.62 -990.46 1683.78-2862.42 
> 4866.12-8272.4014063.08********40642.31
> ********************************
>
>
> Compare
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
>
>
> On Mon, Jul 22, 2019 at 12:19 AM Rui Barradas <ruipbarradas using sapo.pt 
> <mailto:ruipbarradas using sapo.pt>> wrote:
>
>     Simpler, no loops:
>
>
>     print0b <- function(x, len = 10, digits = 2, fill = ""){
>        n <- length(x)
>        x <- round(x, digits = digits)
>        m <- n %/% len
>        remainder <- n %% len
>        A <- matrix(x[seq_len(len*m)], ncol = len)
>        if(remainder > 0){
>          A <- rbind(A, c(x[(len*m + 1):n], rep(fill, len*(m + 1) - n)))
>        }
>        A
>     }
>
>
>     Hope this helps,
>
>     Rui Barradas
>
>     Às 07:47 de 22/07/19, Rui Barradas escreveu:
>     > Hello,
>     >
>     > Maybe something like the following is what you want.
>     > I have added an extra argument 'fill' to allow to choose what to
>     print
>     > in the end. It's default value is "" making the entire matrix
>     elements
>     > characters but it can be NA or 0.
>     >
>     > print0 <- function(x, len = 10, digits = 2, fill = ""){
>     >    n <- length(x)
>     >    x <- round(x, digits = digits)
>     >    passes <- n %/% len
>     >    remainder <- n %% len
>     >    A <- matrix(fill, nrow = passes + (remainder > 0), ncol = len)
>     >    for(i in seq_len(passes)){
>     >      A[i, ] <- x[(len*(i - 1) + 1):(len*i)]
>     >    }
>     >    A[nrow(A), 1:remainder] <- x[(len*passes + 1):n]
>     >    A
>     > }
>     >
>     > print0(rnorm(23), 10)
>     > print0(rnorm(23), 10, fill = 0)
>     >
>     >
>     > Hope this helps,
>     >
>     > Rui Barradas
>     >
>     > Às 21:34 de 20/07/19, Steven escreveu:
>     >> 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]]
>     >>
>     >> ______________________________________________
>     >> R-help using r-project.org <mailto: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.
>     >>
>     >
>     > ______________________________________________
>     > R-help using r-project.org <mailto: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.
>
>     ______________________________________________
>     R-help using r-project.org <mailto: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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list