Printing with cat in a procedure

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Printing with cat in a procedure

Steven Yen
How do I print a matrix running a procedure? In the code below, I print
with the cat command and get a vector (from A and C).

A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T)
B<-diag(4)

try5<-function(A,B){
  C<-A+B
  cat("\nA =",A,"\nC = ",C)
structure(list(A=A,B=B,C=C))
}

v<-try5(A,B)
v$C

--
[hidden email] (S.T. Yen)


        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Printing with cat in a procedure

Eric Berger
Hi Steven,
Here's one way, using print

try5<-function(A,B){
  C<-A+B
  #cat("\nA =",A,"\nC = ",C)
  cat("\nA = ")
  print(A)
  cat("\nC = ")
  print(C)
  structure(list(A=A,B=B,C=C))
}

HTH,
Eric


On Sat, Dec 22, 2018 at 4:32 PM Steven Yen <[hidden email]> wrote:

> How do I print a matrix running a procedure? In the code below, I print
> with the cat command and get a vector (from A and C).
>
> A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T)
> B<-diag(4)
>
> try5<-function(A,B){
>   C<-A+B
>   cat("\nA =",A,"\nC = ",C)
> structure(list(A=A,B=B,C=C))
> }
>
> v<-try5(A,B)
> v$C
>
> --
> [hidden email] (S.T. Yen)
>
>
>         [[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.
>

        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Printing with cat in a procedure

Rui Barradas
In reply to this post by Steven Yen
Hello,

Use print(A) and print(C). cat is meant for simpler objects.

Hope this helps,

Rui Barradas

Às 14:31 de 22/12/2018, Steven Yen escreveu:

> How do I print a matrix running a procedure? In the code below, I print
> with the cat command and get a vector (from A and C).
>
> A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T)
> B<-diag(4)
>
> try5<-function(A,B){
>    C<-A+B
>    cat("\nA =",A,"\nC = ",C)
> structure(list(A=A,B=B,C=C))
> }
>
> v<-try5(A,B)
> v$C
>

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Printing with cat in a procedure

Jeff Newmiller
In reply to this post by Steven Yen
Try using print instead of cat [1], and please read about what the arguments are in the help file [2][3] for any function you are using before posting a question.

[1] https://stackoverflow.com/questions/31843662/what-is-the-difference-between-cat-and-print
[2] ?cat
[3] ?print

On December 22, 2018 6:31:52 AM PST, Steven Yen <[hidden email]> wrote:

>How do I print a matrix running a procedure? In the code below, I print
>
>with the cat command and get a vector (from A and C).
>
>A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T)
>B<-diag(4)
>
>try5<-function(A,B){
>  C<-A+B
>  cat("\nA =",A,"\nC = ",C)
>structure(list(A=A,B=B,C=C))
>}
>
>v<-try5(A,B)
>v$C

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Printing with cat in a procedure

Steven Yen
In reply to this post by Eric Berger
Thank you all - print works wonders.

On 12/22/2018 10:36 PM, Eric Berger wrote:

> Hi Steven,
> Here's one way, using print
>
> try5<-function(A,B){
>   C<-A+B
>   #cat("\nA =",A,"\nC = ",C)
>   cat("\nA = ")
>   print(A)
>   cat("\nC = ")
>   print(C)
>   structure(list(A=A,B=B,C=C))
> }
>
> HTH,
> Eric
>
>
> On Sat, Dec 22, 2018 at 4:32 PM Steven Yen <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     How do I print a matrix running a procedure? In the code below, I
>     print
>     with the cat command and get a vector (from A and C).
>
>     A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T)
>     B<-diag(4)
>
>     try5<-function(A,B){
>       C<-A+B
>       cat("\nA =",A,"\nC = ",C)
>     structure(list(A=A,B=B,C=C))
>     }
>
>     v<-try5(A,B)
>     v$C
>
>     --
>     [hidden email] <mailto:[hidden email]> (S.T. Yen)
>
>
>             [[alternative HTML version deleted]]
>
>     ______________________________________________
>     [hidden email] <mailto:[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] (S.T. Yen)


        [[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.