[R] Difference between console output of cat and print

Bert Gunter bgunter.4567 at gmail.com
Tue Apr 18 00:30:27 CEST 2017


Well,...

cat() is as Jeff describes.

However, print() is a generic function (see ?UseMethod) for which
there are literally hundreds of different methods that may do far
more/different than merely output character strings. For example, the
print method for trellis objects, print.trellis, draws a graph of the
object.

The print method  called for print(10) is the default method, for
which ?print.default should be consulted: it is actually printing a
vector of length 1, and the print method for vectors labels each line
with the index of the first item printed.

Please read An Intro to R or other R tutorial to learn about S3 methods.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Apr 17, 2017 at 3:01 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> Please stop posting html email per the Posting Guide. You are only going to reduce the chance of successfully communicating your questions to experienced users on this list.
>
> Re cat vs print: the purpose of print is to show values much as they are entered in source code, so quotes and escaped characters such as "\n" are shown. Cat is intended to provide a way to send characters straight to the console so the effects of special characters can be visible (i.e. getting text on the next line when a "\n" occurs in a string). Thus the element numbering is not relevant there.
> --
> Sent from my phone. Please excuse my brevity.
>
> On April 17, 2017 12:18:36 AM PDT, Data MagicPro <datamagicpro2017 at gmail.com> wrote:
>>Since both *cat * as well as * print * create a character vector for
>>outputing on the screen. Still both give different results as apparant
>>below. My query is why so ?
>>
>>
>>> cat(10)
>>10
>>> print(10)
>>[1] 10
>>
>>Why is the [1] of index number missing in case of *cat *?
>>
>>Thanks
>>Ramnik
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at 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 at 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.



More information about the R-help mailing list