Skip to content

Data Science Tutorials

For Data Science Learners

  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • How to use image function in R
    How to use the image function in R R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? R
Separate a data frame column into multiple columns

Separate a data frame column into multiple columns-tidyr Part3

Posted on July 21July 20 By Admin No Comments on Separate a data frame column into multiple columns-tidyr Part3

Separate a data frame column into multiple columns, To divide a data frame column into numerous columns, use the separate() function from the tidyr package.

How to Use Gather Function in R?-tidyr Part2

The basic syntax used by this function is as follows.

separate(data, col, into, sep)

where:

data: Name of the data frame

col: Name of the column to separate

into: a list of names to divide the column into

sep: The amount to use as a column separator is

Separate a data frame column into multiple columns

The practical application of this function is demonstrated in the examples that follow.

Example 1: Dividing a column into two

Let’s say we have the R data frame shown below.

Let’s create a data frame

df <- data.frame(player=c('P1', 'P1', 'P2', 'P2', 'P3', 'P3'),
year=c(1, 2, 1, 2, 1, 2),
stats=c('25-2', '22-3', '28-5', '21-9', '22-5', '29-3'))

Now we can view the data frame

Best Books to learn Tensorflow – Data Science Tutorials

df
   player year stats
1     P1    1  25-2
2     P1    2  22-3
3     P2    1  28-5
4     P2    2  21-9
5     P3    1  22-5
6     P3    2  29-3

The stats column can be divided into two new columns labelled “points” and “assists” using the separate() function as follows:

library(tidyr)

divide the stats column into columns for points and assists.

separate(df, col=stats, into=c('points', 'assists'), sep='-')
   player year points assists
1     P1    1     25       2
2     P1    2     22       3
3     P2    1     28       5
4     P2    2     21       9
5     P3    1     22       5
6     P3    2     29       3

Example 2: Column Should Be Divided Into More Than Two Columns

The stats column can be divided into three distinct columns using the separate() function as follows.

dplyr Techniques and Tips – Data Science Tutorials

library(tidyr)
df <- data.frame(player=c('P1', 'P1', 'P2', 'P2', 'P3', 'P3'),
year=c(1, 2, 1, 2, 1, 2),
stats=c('25-2-3', '22-3-3', '28-5-3', '21-9-2', '22-5-1', '29-3-0'))
df
player year  stats
1     P1    1 25-2-3
2     P1    2 22-3-3
3     P2    1 28-5-3
4     P2    2 21-9-2
5     P3    1 22-5-1
6     P3    2 29-3-0

Stats column is split into three new columns.

separate(df, col=stats, into=c('points', 'assists', 'steals'), sep='-')
player year points assists steals
1     P1    1     25       2      3
2     P1    2     22       3      3
3     P2    1     28       5      3
4     P2    2     21       9      2
5     P3    1     22       5      1
6     P3    2     29       3      0

Check your inbox or spam folder to confirm your subscription.

How to do Conditional Mutate in R? – Data Science Tutorials

Have you liked this article? If you could email it to a friend or share it on Facebook, Twitter, or Linked In, I would be eternally grateful.

Please use the like buttons below to show your support. Please remember to share and comment below. 

R Tags:tidyr

Post navigation

Previous Post: How to Use Gather Function in R?-tidyr Part2
Next Post: Convert multiple columns into a single column-tidyr Part4

Related Posts

  • Best Data Visualization Books Course
  • How to Specify Histogram Breaks in R R
  • Best Git Books R
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Extract patterns in R
    Extract patterns in R? R
  • Hypothesis Testing in R Programming
    Hypothesis Testing in R Programming R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Calculating Conditional Probability in R
    Calculating Conditional Probability in R R
  • Jarque-Bera Test in R
    Jarque-Bera Test in R With Examples R
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • Cross-validation in Machine Learning
    Cross-validation in Machine Learning Statistics
  • Convert a continuous variable to a categorical in R R
  • Confidence Intervals in R
    Confidence Intervals in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme