How to Split Column Into Multiple Columns in R DataFrame?

12 Jan, 2022
In this article, we will discuss how to split a column from a dataframe into multiple columns in the R programing 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 framedf <- data.frame(Name=c('Priyank Mishra', 'Abhiraj Srivastava',                        'Pawananjani Kumar'),                 State= c("Uttar Pradesh", "Maharashtra", "Bihar")) print(" Data frame before splitting: ")df # load stringr librarylibrary(stringr) # Split name column into firstname and last namedf[c('First Name', 'Last Name')] <- str_split_fixed(df$Name, ' ', 2) # Rearrange columns and remove original name columndf <- 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
To split a column into multiple columns in the R Language, we use the separator() function of the dplyr package library. The separate() function separates a character column into multiple columns with a regular expression or numeric locations. The function takes input character vector as an argument and the output column names in a vector as an argument and returns final data vector.
Syntax:
separate( sample_data, col )
Parameter:
sample_data: determines the input data frame column.
col: determines the final columns that it has to be separated.
Example: Split column into multiple columns
R
# create sample data framedf <- data.frame(Name=c('Priyank Mishra', 'Abhiraj Srivastava',                        'Pawananjani Kumar'),                 State= c("Uttar Pradesh", "Maharashtra", "Bihar")) print(" Data frame before splitting: ")df # load dplyr and tidyr librarylibrary(dplyr)library(tidyr) # Split name column into firstname and last namedf <- df %>% separate(Name, c('First Name', 'Last Name')) 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
sweetyty
simmytarika5
sagartomar9927
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
Group by function in R using Dplyr
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
How to filter R DataFrame by values in a column?
Remove rows with NA in one column of R DataFrame
Replace Specific Characters in String in R