The str_glue() function in R’s stringr package is “used to concatenate strings together, often using variables within the string.”
Syntax
str_glue(..., .sep, .envir = parent.frame(), .x, .na = "NA")
Parameters
- …: These are the strings to be concatenated. Strings can be a mix of constants and expressions enclosed in {}. The expressions within {} will be evaluated, and their results will be inserted into the string.
- .sep: Separator used to separate elements.
- .envir: The environment in which to evaluate the expressions. The default is the parent frame.
- .x: It is an environment, list, or data frame used to look up values.
- .na: A character vector of length 1 to replace NA values. The default is “NA”.
Return value
It returns a character vector with the same length as the longest input.
Example 1: Simple String Concatenation
library(stringr)
result <- str_glue("Hello", " ", "World!")
print(result)
Output
Hello World!
Example 2: Using Variables
library(stringr)
name <- "John"
age <- 25
result <- str_glue("My name is {name} and I am {age} years old.")
print(result)
Output
My name is John and I am 25 years old.
Example 3: Handling NA Values
library(stringr)
value <- NA
result <- str_glue("The value is {value}.", .na = "missing")
print(result)
Output
The value is missing.
Remember that the str_glue() function is handy when combined with data manipulation packages like dplyr, allowing you to create new variables in a data frame using string interpolation.
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.