Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Checking Missing Values in R
    Checking Missing Values in R R
  • Top 7 Skills Required to Become a Data Scientist
    Top 7 Skills Required to Become a Data Scientist Machine Learning
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • How to create a heatmap in R
    How to create a heatmap in R R
  • The Multinomial Distribution in R
    The Multinomial Distribution 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 Jim No Comments on Separate a data frame column into multiple columns-tidyr Part3
Tweet
Share
Share
Pin

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. 

Tweet
Share
Share
Pin
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

  • How to Use Bold Font in
    How to Use Bold Font in R with Examples R
  • droplevels in R with examples
    droplevels in R with examples R
  • Comparing group means in R
    One way ANOVA Example in R-Quick Guide R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • How to create a heatmap in R
    How to create a heatmap in R R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R 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
  • Statistical test assumptions and requirements
    Statistical test assumptions and requirements Statistics
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme