String split of the column in R

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

Let’s first create the dataframe

df1 = data.frame(State = c('Arizona AZ USA','Georgia GG USA', 'Newyork NY USA','Indiana IN USA','Florida FL USA'),
Score=c(62,47,55,74,31))
df1

df1 will be

String split of the column in R 1

 

String split of the column in R:

str_split_fixed() is used to split the column , argument 2 is the number of splits

library(stringr)

df_two_splits= str_split_fixed(df1$State," ",2)
df_two_splits

Result:

String split of the column in R 2

 

str_split_fixed() is used to split the column , argument 3, indicates 3 splits.

library(stringr)

df_three_splits=str_split_fixed(df1$State," ",3)
df_three_splits

Result:

String split of the column in R 3

                                                                                             

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.