How to Split Dataframe Column into Multiple Columns in R

Here are two ways to split the data frame column into multiple columns in R:

  1. Using tidyr::separate()
  2. Using stringr::str_split_fixed()

Method 1: Using tidyr::separate()

Syntax

separate(df, col)

Parameters

  1. df: It determines the input data frame column.
  2. col: It determines the final columns that it has to be separated.

Install and load “dplyr” package

install.packages("tidyr")

library(tidyr)

Visual representation

Method 1 - Using separate()

Example

library(tidyverse)

# Create sample data
df <- data.frame(
  id = 1:4,
  combined_values = c("A-1", "B-2", "C-3", "D-4")
)

# Print the original data
print(df)

# Split the 'combined_values' column into two columns 'letter' and 'number'
split_df <- data %>%
         separate(combined_values, into = c("letter", "number"), sep = "-")

# Print the updated data
print(split_df)

Output

Output of separate()

The number of “into” columns in separate() should match the number of elements you expect to split.

Method 2: Using stringr::str_split_fixed()

Syntax

str_split_fixed(sample_string, separator_pattern, n)

Parameters

  1. sample_string: It determines the input character vector.
  2. separator_pattern: It determines the pattern to split up by, as defined by a POSIX regular expression.
  3. n: It determines the number of parts the string has to be divided into.

Install and load “stringr” package

install.packages("stringr")

library(stringr)

Visual representation

Method 2 - Using str_split_fixed()

Example

library(stringr)

# Create sample data
df <- data.frame(
  id = 1:4,
  combined_values = c("A-1", "B-2", "C-3", "D-4")
)

# Print the original data
df

df[c("letter", "number")] <- str_split_fixed(df$combined_values, "-", 2)

df_split <- df[c("id", "letter", "number")]

df_split

Output

Output of Using str_split_fixed()

That’s it!

Leave a Comment