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:
- Convert Character String to Variable Name
- Print Character String to Newline of RStudio Console
- R Programming Examples
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.
Statistics Globe Newsletter
2 Comments. Leave new
What if you want to print your variable with a percentage symbol (i.e. 99999%). I’m using cat(sprintf(x, y) in a for loop, but it won’t work if my character strings have the % symbol in them. In my character strings I have:
example <- 'This is an example of how I'd like to include %s % of my variables as percentages. \n\n'
Hello Vanessa,
Sorry for the late response. Have you solved your problem already? If not, could you please share your code. Then I can check what is wrong.
Regards,
Cansu