Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Convert multiple columns into a single column-tidyr Part4 appeared first on Data Science Tutorials

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

Best Books to learn Tensorflow – Data Science Tutorials