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
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.