How to Split Column Into Multiple Columns in R DataFrame?
Last Updated :
29 Dec, 2022
In this article, we will discuss how to split a column from a data frame into multiple columns in the R programming Language.
Method 1: Using str_split_fixed() function of stringr package library
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
Method 2: Using separate() function of dplyr package library
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 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 dplyr and tidyr library
library(dplyr)
library(tidyr)
# Split name column into firstname and last name
df <- 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
Similar Reads
Split DataFrame Variable into Multiple Columns in R In this article, we will discuss how to split dataframe variables into multiple columns using R programming language. Method 1: Using do.call method The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into
3 min read
How to select multiple DataFrame columns by name in R ? In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Sy
1 min read
How to add suffix to column names in R DataFrame ? Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read
How to Switch Two Columns in R DataFrame? In this article, we will discuss how to switch two columns in dataframe in R Programming Language. Let's create the dataframe with 6 columns R # create a dataframe data = data.frame(column1=c(1, 2, 3), column2=c(4, 5, 6), column3=c(2, 3, 4), column4=c(4, 5, 6), column5=c(5, 3, 2), column6=c(2, 3, 1)
1 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read