# Printing vector

12 messages
Open this post in threaded view
|

## Printing vector

 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Printing vector

 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Printing vector

 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]] > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Printing vector

Open this post in threaded view
|

## Re: Printing vector

Open this post in threaded view
|

## Re: Printing vector

Open this post in threaded view
|

## Re: Printing vector

Open this post in threaded view
|

## Re: Printing vector

Open this post in threaded view
|

## Re: Printing vector

 In reply to this post by Steven Yen-2 Dear Steven, The function "write()" has a parameter "columns". And sprint() can do do some formatting in C style. x <- rnorm(100) s <- sprintf( fmt="%8.2f" ,x ) write(s,  file="", ncolumns=7L) Cheers, Lei On Mon, 22 Jul 2019 at 07:37, Steven <[hidden email]> wrote: > > 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]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|