15.1 C
London
Friday, July 5, 2024
HomeStatistics TutorialRHow to Use str_split in R (With Examples)

How to Use str_split in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The str_split() function from the stringr package in R can be used to split a string into multiple pieces. This function uses the following syntax:

str_split(string, pattern)

where:

  • string: Character vector
  • pattern: Pattern to split on

Similarly, the str_split_fixed() function from the stringr package can be used to split a string into a fixed number of pieces. This function uses the following syntax:

str_split_fixed(string, pattern, n)

where:

  • string: Character vector
  • pattern: Pattern to split on
  • n: Number of pieces to return

This tutorial provides examples of how to use each of these functions on the following data frame:

#create data frame
df frame(team=c('andy & bob', 'carl & doug', 'eric & frank'),
                 points=c(14, 17, 19))

#view data frame
df

          team points
1   andy & bob     14
2  carl & doug     17
3 eric & frank     19

Example 1: Split String Using str_split()

The following code shows how to split the string in the “team” column using the str_split() function:

library(stringr)

#split the string in the team column on " & "
str_split(df$team, " & ")

[[1]]
[1] "andy" "bob" 

[[2]]
[1] "carl" "doug"

[[3]]
[1] "eric"  "frank"

The result is a list of three elements that show the individual player names on each team.

Example 2: Split String Using  str_split_fixed()

The following code shows how to split the string in the “team” column into two fixed pieces using the str_split_fixed() function:

library(stringr)

#split the string in the team column on " & "
str_split_fixed(df$team, " & ", 2)

     [,1]   [,2]   
[1,] "andy" "bob"  
[2,] "carl" "doug" 
[3,] "eric" "frank"

The result is a matrix with two columns and three rows.

Once useful application of the str_split_fixed() function is to append the resulting matrix to the end of the data frame. For example:

library(stringr)

#split the string in the team column and append resulting matrix to data frame
df[ , 3:4] #view data frame
df
          team points   V3    V4
1   andy & bob     14 andy   bob
2  carl & doug     17 carl  doug
3 eric & frank     19 eric frank

The column titled ‘V3’ shows the name of the first player on the team and the column titled ‘V4’ shows the name of the second player on the team.

Additional Resources

How to Use str_replace in R
How to Perform Partial String Matching in R
How to Convert Strings to Dates in R
How to Convert Character to Numeric in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories