The paste command is the topic of a video I made a while back. In it, I demonstrate how to use the paste command to concatenate a string of different values.
That video is fine, but the real power in the paste command in my opinion is its ability to assemble strings as labels for plots and reports
Imagine you have a graph with a line on it, and you want to label the graph with the slope of the line, or with the correlation coefficient. This will change with the underlying data, so you want a label that provides you a refreshable variable.
The Paste Command Basic Syntax
As you probably know by now, when you run equations in R code in R GUI, the answer prints to the console. If you want to keep the result of the equation for the future, you have to store them in a named object, and use the arrow command.
And as you probably already also know by now, storing results in objects in R and then calling them up later makes your life easier when you are not very good at programming, like me. As an example, in this blog post, I show you how to save different hexadecimal color codes as a string called cool_colors, and then use that string while making a plot so that you can overwrite the default colors with the cool colors!
So the paste() command is basically your hack for storing report and plot labels with values in them from your data, and then calling up the label later in your report or plot (or both! It’s recyclable!). First, let’s just look at some simple paste() code from my other video (available on Github).
Let’s start by looking at code that creates an object called PhoneNumber through pasting a few numeric sequences together (three arguments in total), followed by the sep=”” argument, which always comes at the end of paste(). The sep=”” option tells R to put “nothing” between each argument.
PhoneNumber <- paste(612,781,8888,sep="")
So as you can guess, when I run this code, you will get an object called PhoneNumber. If you run that object, the value of it will be 6127818888.
The Paste Command for Refreshable Labels for Plots and Reports
In my example above, all those arguments are hardcoded into the paste() string. Now, let’s shift our thinking to ways we can include arguments that are results of calculations done on the data – such as a correlation coefficient, as I mentioned earlier! We basically could run that calculation, and put it in the string as part of a label. If we save that string as an object, then we can call up and reuse that object as a label on plots and reports.
In my tutorial video on the topic, I show an example of taking a dataset called lineitems with a numeric variable called Tot_Cost (meaning “total cost”) and finding the maximum value of the column by doing max(lineitems$Tot_Cost). Then, I include the code below to create a label for a report.
report_label <- paste("The maximum total cost is ", max(lineitems$Tot_Cost), "!!!!", sep="")
As you can see, the code makes the object called report_label, and includes the calculation as the second argument. See how the string comes out when I run report_label to the console:
"The maximum total cost is 293.88!!!!"
Updated October 12, 2022. Revised banners and updated June 16, 2023.
Read all of our data science blog posts!
The paste command in R is used to concatenate strings. You can leverage the paste command to make refreshable label objects for reports and plots, as I describe in my blog post.