Print Function in R

The print() function in R is essential for displaying the results of your computations on the screen.

print(x)

Here, 'x' in the parentheses represents the item you intend to show.

Capable of handling a variety of data types, print() can display numbers, text strings, variable contents, or even results from expressions and functions.

Additionally, print() offers extra arguments for customizing the display of objects. These are optional and not commonly used but are valuable for tailored requirements. We will delve into these in subsequent examples.

Let's consider an example: displaying the phrase "Hello World!" using print(). In R, you simply place the phrase in quotes and pass it as an argument to print().

> print("Hello World!")
[1] "Hello World!"

Printing a numerical value follows a similar straightforward process, without the need for quotes.

> print(2010)
[1] 2010

The print() function is also adept at executing calculations. Insert a mathematical expression as the argument, and print() will compute and display the result.

> print(16/2)
[1] 8

Beyond expressions, print() is equally effective for outputting variable contents.

> v=2010
> print(v)
[1] 2010

As hinted at the outset, print() includes optional parameters for enhanced functionality. For instance, the digits argument controls the number of decimal places in a number's display.

> print(3.14159, digits = 2)
[1] 3.14

While these additional arguments offer refined control over how data is presented, the standard usage of print() generally suffices for most applications.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin