25.5 C
London
Thursday, June 19, 2025
HomeStatistics TutorialRHow to Print Multiple Variables on the Same Line in R

How to Print Multiple Variables on the Same Line in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the cat() function to easily print multiple variables on the same line in R.

This function uses the following basic syntax:

cat(variable1, variable2, variable3, ...)

The following examples show how to use this syntax in different scenarios.

Example 1: Print Character String & Variable on Same Line

The following code shows how to use the cat() function to print a character string and several numeric variables on the same line:

#define character string
my_text #define numeric variables
my_value1 #print character string and numeric variables on the same line
cat(my_text, my_value1, "or", my_value2)

The answer is 5 or 10

Each of the variables are printed on the same line.

Example 2: Print Multiple Variables on Same Line with No Text

The following code shows how print several variables from some function on the same line without any text:

#define function
do_stuff function(x) {
    x2 #use function
do_stuff(5)

10 15 20

The function returns all three numeric variables on the same line without any text explaining which variable names correspond to each value.

Example 3: Print Multiple Variables on Same Line with Text

The following code shows how print several variables from some function on the same line with text:

#define function
do_stuff function(x) {
    x2 #use function
do_stuff(5)

x2 = 10 x3 = 15 x4 = 20

The function returns all three numeric variables on the same line with text explaining which variable names correspond to each value.

Example 4: Print Multiple Variables on New Lines with Text

The following code shows how to use the n operator within the cat() function to print several variables from some function on new lines with text:

#define function
do_stuff function(x) {
    x2 #use function
do_stuff(5)

x2 = 10 
x3 = 15 
x4 = 20

The function returns all three variables on different lines with text explaining which variable names correspond to each value.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Use sprintf Function in R to Print Formatted Strings
How to Print All Rows of a Tibble in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories