Getting to know glue
1. Getting to know glue
Alright, you now know the basics of how to extract substrings from a larger amount of text. For the next three lessons we'll focus on another step in the process of analyzing and manipulating text: Creating text from data.2. Pasting is tedious
You've worked with R before. I'd say, chances are high, you've also worked with paste. Paste is a base R function that binds two or more strings together. So when we pass the two arguments "Hi" and a username, which - in this example - holds another string "Adam", we will get back "Hi Adam". In this course, we will be using the "glue" function for that. The reason why we will be using glue is that it is much easier to read when we have a lot of variables and text to concatenate. Plus, while paste can only display variables that are available in the global scope, glue will help us create temporary variables and keep our global scope clean. Both the function as well as the package that it comes from, are called glue. Luckily, glue is already part of the tidyverse package, so chances are, that it's already installed in some of your projects.3. What you pass to glue
You'll quickly get used to working with glue. There is only one function parameter that you might occasionally use, which is "dot NA". It is used in the case that one of the variables that you use in your text, is not available. But the very core of the function is the first string that you pass to it. It is sometimes also called a template string. Let's inspect it a little closer.4. What is a template string?
The template string is a string in which we use curly braces to tell glue that it should interpret its content like code. So when we write a variable like "username 1" in it, glue will look up the content of that variable and print it at that position of the text. Inside of the curly braces we can place pretty much all sorts of code - as long as it can be converted to a character. So if we write for example two plus two inside the curly braces and pass that to glue, it will successfully print out the result of the content within the curly braces, which is four. The number four will be converted into a character. This concept of putting code inside of longer strings is also pretty common in other programming languages like Javascript or Python.5. Temporary variables
Occasionally you will run into the situation where you would like to output a temporary variable - by temporary I mean a value that you have not calculated and stored into a variable yet. And one that you also don't need anywhere else but really just to create this meaningful sentence once. If that's the case, you can create a variable on the fly by passing it as a function argument. So in this example we pass "length" equals fifty as a second argument. The variable "length" is created just at the moment of the execution of the glue function and won't be stored in the global environment. With this approach we can pass relatively complex calculations to glue without sacrificing the readability of our template string. Our colleagues and also our future selves will understand very quickly, what we are printing out.6. Glue inside data frames
Glue can also be used inside a "mutate" to create a new column. Mutate is a function from the dplyr package, which is also part of the tidyverse package collection. You don't need to know a lot about the dplyr package. In this course, it's enough that you know that here mutate creates a new column in the data frame "df".7. Let's practice!
Alright, let's put that into practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.