Search code examples
routput-formatting

How can I make vertical output instead of horizontal output?


I am trying to make the horizontal output from a particular function into a vertical output and I am trying to figure out with the "cat" function but could not find the way to do so. The function that I am concerned is the following:

 context <- function (a,b,c)
 {
 U=matrix(0, nrow = c, ncol = c)
 t = 0.4
 m = 0.2
 for (i in 1:c)
 { 
 U[i] = t*a + b/i - 0.224
 }
 cat(U)
 }
 context(2,3,7)

The output that I get is like:

 3.576 2.076 1.576 1.326 1.176 1.076 1.004571 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

However, I do not know I to make this output in the vertical way by manipulating the function itself. I would greatly appreciate if I could get help on this.

Thank you very much in advance!


Solution

  • I don't understand why you create all those zeros.

    Maybe you'd like to return a matrix?

    context <- function (a,b,c)
    {
      t = 0.4
      m = 0.2
      U = t*a + b/seq_len(c) - 0.224
      as.matrix(U)
    }
    
    context(2,3,7)
    
    #         [,1]
    #[1,] 3.576000
    #[2,] 2.076000
    #[3,] 1.576000
    #[4,] 1.326000
    #[5,] 1.176000
    #[6,] 1.076000
    #[7,] 1.004571