The glue package in R provides a lot of great tools for using regular expressions and manipulating data. In this post, we will look at examples of using just the glue() function from this package.
Paste vs Glue
The paste() function is an older way of achieving the same things that we can achieve with the glue() function. paste() allows you to combine strings. Below we will load our packages and execute a command with the paste() function.
> library(glue)
> library(dplyr)
> people<-"Dan"
> paste("Hello",people)
[1] "Hello Dan"
In the code above, we load the glue and the dplyr package (we will need dplyr later). We then create an object called “people” that contains the string “Dan”. We then used the past function to combine the “people” vector with the string “Hello”. The output is at the bottom of the code.
Below is an example of the same output but using the glue() function
> glue("Hello {people}")
Hello Dan
Inside the glue() function everything is inside parentheses. However, the object “people” is inside curly braces and this indicates to the glue() function to look for what “people” represents. The printout is the same but without parentheses.
Multiple Strings
Below is an example of including multiple strings in the same glue() function
> people<-"Dan"
> people_2<-"Darrell"
> glue("Hello {people} and {people_2}")
Hello Dan and Darrell
In the first two lines above we make our objects. In line 3 we used the glue() function again and inside we included both objects in curly braces.
In another example using multiple strings we will replace text if it meets a certain criteria.
> people<-"Dan"
> people_2<-NA
> glue("Hi {people} and {people_2}",.na="What")
Hi Dan and What
In the code above we start by creating two objects. The second object (people_2) has stored NA. The code in the third line is the same with the exception of the “.na” argument. The “.na” argument is set to the string “What” which tells R to replace any NA values with the string “What”. The output is in the final line.
Temporary Variables
It is also possible to make variables that are temporary. The temporary variable can be named or unnamed. Below is an example with a named variable.
> glue("Dan is {height} cm tall.",height=175)
Dan is 175 cm tall.
The temporary variable “height” is inside the curly braces. The value for “height” is set inside the function to 175.
It is also possible to have unnamed variables inside the function. Below we will use a function inside the curly braces.
> glue("The average number is {mean(c(2,3,4,5))}")
The average number is 3.5
The example is self-explanatory. We used the mean() function inside the curly braces to get a calculated value. As you can see, the potential is endless.
Using Dataframes
In our last example, we will see how you can create a data frame and using input from one column to create a new column.
> df<-data.frame(column_1="Dan")
> df
column_1
1 Dan
> df %>% mutate(new_column = glue("Hi {column_1}"))
column_1 new_column
1 Dan Hi Dan
Here is what we did.
- We made a dataframe called df. This dataframe contains one column called column_1. In column_1 we have the string Dan.
- In line 2 we display the values of the dataframe.
- Next, we use the mutate() function to create a new column. Inside the mutate function we use the glue function and set it to create a string that uses the word “Hi” in front of the values of column_1.
- Lastly, we print out the results.
Conclusion
The glue package provides many powerful tools for manipulating data. The examples provided here only focus on one function. As such, this package is sure to provide useful ways in which data analyst can modify their data.
