Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • What is the best way to filter by row number in R?
    What is the best way to filter by row number in R? R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • How to perform TBATS Model in R
    How to perform TBATS Model in R R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • How to get the last value of each group in R
    How to get the last value of each group in R R
Convert multiple columns into a single column

Convert multiple columns into a single column-tidyr Part4

Posted on July 22July 17 By Jim No Comments on Convert multiple columns into a single column-tidyr Part4
Tweet
Share
Share
Pin

Convert multiple columns into a single column, To combine numerous data frame columns into one column, use the union() function from the tidyr package.

Convert multiple columns into a single column

The basic syntax used by this function is as follows.

unite(data, col, into, sep)

where:

data: Name of the data frame

col: Name of the new united column

… : names for the columns to be combined in a vector

sep: How to create a new united column and combine the data

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

5 Free Books to Learn Statistics For Data Science – Data Science Tutorials

Example 1: Unite Two Columns into One Column

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),
points=c(202, 290, 180, 101, 312, 219),
assists=c(92, 63, 76, 88, 65, 52))

Now we can view the data frame

df
  player year points assists
1     P1    1    202      92
2     P1    2    290      63
3     P2    1    180      76
4     P2    2    101      88
5     P3    1    312      65
6     P3    2    219      52

The “points” and “assists” columns can be combined into a single column using the union() function.

library(tidyr)

combine the columns for points and assists into one column.

Best Books on Data Science with Python – Data Science Tutorials

unite(df, col='points-assists', c('points', 'assists'), sep='-')
  player year points-assists
1     P1    1         202-92
2     P1    2         290-63
3     P2    1         180-76
4     P2    2         101-88
5     P3    1         312-65
6     P3    2         219-52

Example 2: Unite More Than Two Columns

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

Let’s create a data frame

df2 <- data.frame(player=c('P1', 'P1', 'P2', 'P2', 'P3', 'P3'),
year=c(1, 2, 1, 2, 1, 2),
points=c(228, 229, 198, 151, 412, 325),
assists=c(82, 93, 66, 45, 89, 95),
blocks=c(28, 36, 32, 82, 18, 12))

Let’s view the data frame

df2
  player year points assists blocks
1     P1    1    228      82     28
2     P1    2    229      93     36
3     P2    1    198      66     32
4     P2    2    151      45     82
5     P3    1    412      89     18
6     P3    2    325      95     12

The points, assists, and blocks columns can be combined into a single column using the union() function.

library(tidyr)

combine the columns for scoring, assists, and blocks into one

unite(df2, col='stats', c('points', 'assists', 'blocks'), sep='/')
player year     stats
1     P1    1 228/82/28
2     P1    2 229/93/36
3     P2    1 198/66/32
4     P2    2 151/45/82
5     P3    1 412/89/18
6     P3    2 325/95/12

Check your inbox or spam folder to confirm your subscription.

Best Books to learn Tensorflow – 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: Separate a data frame column into multiple columns-tidyr Part3
Next Post: How to Create Summary Tables in R

Related Posts

  • How to Use Bold Font in
    How to Use Bold Font in R with Examples 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
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • Radar plot in R
    How to create Radar Plot in R-ggradar R
  • How to Filter Rows In R
    How to Filter Rows 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
  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to compare variances in R
    How to compare variances in R R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme