Repeat the string of the column in R

To Repeat the string of the column in R, we use str_dup() function of “stringr” package. In this tutorial we will be looking on how to repeat the string of column in R with an example

Let’s first create the dataframe

df1 = data.frame(State = c('Arizona','Georgia', 'Newyork','Indiana','Florida'), 
                 Score=c(62,47,55,74,31))
df1

df1 will be

Repeat the string of the column in R 1

 

Repeat the string of the column in R:

str_dup() function of “stringr” package is used to repeat the string column in R

## Repeat the string of the column in R

library(stringr)
df1$repeat_state= str_dup(df1$State,2)
df1

Argument 2 indicates repeating twice, so the resultant dataframe will be

Repeat the string of the column in R 2

                                                                                               

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts