How to use str_split_fixed() function of stringr package library In R Language
To split a column into multiple columns in the R Language, We use the str_split_fixed() function of the stringr package library. The str_split_fixed() function splits up a string into a fixed number of pieces. The function takes string, the term separating the string and number of parts it has to be divided into as arguments and returns the splitted string.
Syntax:
str_split_fixed( sample_string, separator_pattern, n)
Parameter:
- sample_string: determines the input character vector.
- separator_pattern: determines the pattern to split up by, as defined by a POSIX regular expression.
- n: determines the number of part string has to be divided into.
Example: Split column into multiple columns
R
# create sample data frame df <- data.frame (Name= c ( 'Priyank Mishra' , 'Abhiraj Srivastava' , 'Pawananjani Kumar' ), State= c ( "Uttar Pradesh" , "Maharashtra" , "Bihar" )) print ( " Data frame before splitting: " ) df # load stringr library library (stringr) # Split name column into firstname and last name df[ c ( 'First Name' , 'Last Name' )] <- str_split_fixed (df$Name, ' ' , 2) # Rearrange columns and remove original name column df <- df[ c ( 'First Name' , 'Last Name' , 'State' )] print ( " Data frame after splitting: " ) df |
Output:
Data frame before splitting: Name State 1 Priyank Mishra Uttar Pradesh 2 Abhiraj Srivastava Maharashtra 3 Pawananjani Kumar Bihar Data frame after splitting: First Name Last Name State 1 Priyank Mishra Uttar Pradesh 2 Abhiraj Srivastava Maharashtra 3 Pawananjani Kumar Bihar
How to Split Column Into Multiple Columns in R DataFrame?
In this article, we will discuss how to split a column from a data frame into multiple columns in the R programming Language.