str_dup Function in R (stringr Package)

 

In this R tutorial you’ll learn how to use the str_dup function to duplicate and concatenate strings within a character vector.

The tutorial will contain this content:

So now the part you have been waiting for – the examples:

 

Creation of Example Data

First, we need to create a character string, which we can use in the example of this R tutorial. Consider the following character string in R:

x <- "ABC..."                     # Create example character string

Our example string contains the letters ABC and three dots. Now let’s apply the str_dup function to this character string…

 

Example: Application of str_dup Function in R

Before we can access the functions of the stringr package, we need to install and load the package:

install.packages("stringr")       # Install stringr package
library("stringr")                # Load stringr package

Now, we can use the str_dup function as shown below:

str_dup(x, 5)                     # Apply str_dup function
# "ABC...ABC...ABC...ABC...ABC..."

In the previous R code we specified that we want to duplicate the character string stored in the data object x five times.

Based on the RStudio console output of the previous R code, you can see the result: The str_dup command returned a new character string containing our example string five times in a single concatenated character string.

 

Video, Further Resources & Summary

Do you want to know more about the duplication and concatenation of character strings in R? Then you may have a look at the following video of my YouTube channel. I’m explaining the R programming code of this post in the video:

 

The YouTube video will be added soon.

 

Furthermore, you may have a look at the related tutorials on this homepage. I have released several tutorials already:

 

Summary: In this tutorial you learned how to apply the str_dup function in order to duplicate and concatenate strings within a character vector in the R programming language. In case you have additional questions and/or comments, don’t hesitate to tell me about it in the comments section.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top