Print Character String & Variable on Same Output Line in R (3 Examples)

 

In this tutorial, I’ll show how to return character strings and data objects on the same line to the RStudio console in the R programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the examples!

 

Example Data

We’ll use the following data as basement for this R programming tutorial:

my_string <- "The content of my variable is..."    # Create example string
my_string                                          # Print example string
# "The content of my variable is..."

The previous RStudio console output shows our example character string – The sentence “The content of my variable is…”.

Let’s also create a new variable in R:

my_variable <- 99999                               # Create variable
my_variable                                        # Print variable
# 99999

Our example variable contains the value 99999.

 

Example 1: Printing String & Variable Using print() & paste() Functions

In this Example, I’ll illustrate how to return a string and a data object on the same line in R by using the print and paste functions.

Have a look at the following R code and its output:

print(paste(my_string, my_variable))               # Applying print & paste
# "The content of my variable is... 99999"

The contents of our character string and our variables were returned to the same line in the RStudio console.

 

Example 2: Printing String & Variable Using sprintf() & paste() Functions

The following R code explains how to apply the sprintf function in combination with the paste function to print a character string and the content of a variable to the same output line.

sprintf(paste(my_string, my_variable))             # Applying sprintf & paste
# "The content of my variable is... 99999"

The output is exactly the same as in Example 1. However, this time we used the sprintf function instead of the print function.

 

Example 3: Printing String & Variable Using cat() Function

The following code explains how to return a string and a data object on the same line using the cat function. In this example, we don’t need to use the paste function:

cat(my_string, my_variable)                        # Applying cat
# "The content of my variable is... 99999"

Note that the cat function also returns character strings an easier to read format, since it converts specific character statements directly in the RStudio console. For instance, the character statement “\n” is converted to a newline in the console output.

 

Video, Further Resources & Summary

If you need more information on the R programming syntax of this article, you may want to watch the following video of my YouTube channel. In the video, I show the R syntax of this article.

 

The YouTube video will be added soon.

 

In addition, you could have a look at some of the related tutorials on my website. I have released numerous tutorials already:

 

You learned in this article how to print strings and variables together in the R programming language. If you have additional comments or questions, please tell me about it in the comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top