# combine R Function of dplyr Package (2 Examples)

In this R tutorial you’ll learn how to **apply the combine function** of the dplyr add-on package.

The tutorial looks as follows:

- Example 1: Basic Application of combine() in Comparison to c()
- Example 2: Concatenating Factors with combine Function
- Video & Further Resources

Let’s start right away!

## Example 1: Basic Application of combine() in Comparison to c()

First, we need to install and load the dplyr package to RStudio:

install.packages("dplyr") # Install and load dplyr package library("dplyr") |

install.packages("dplyr") # Install and load dplyr package library("dplyr")

Now, let’s assume that we want to create a simple numeric vector in R. We could do that with the c() function of the base installation of the R programming language:

c(5, 7, 1, 5, 1) # Apply c function # 5 7 1 5 1 |

c(5, 7, 1, 5, 1) # Apply c function # 5 7 1 5 1

However, we could also use the combine function of the dplyr package:

combine(5, 7, 1, 5, 1) # Apply combine function # 5 7 1 5 1 |

combine(5, 7, 1, 5, 1) # Apply combine function # 5 7 1 5 1

Both functions return the same output. So you might say: Why should I even use the combine function?

That’s what I’m going to show you next!

## Example 2: Concatenating Factors with combine Function

The difference between c() and combine() is that the combine function follows the general rules of the tidyverse environment.

These rules are similar to the general rules of Base R. However, there are some differences.

One of these differences occurs when factor vectors should be concatenated. Let’s have a look at this in practice!

First, we need to create two factor vectors in R:

fac1 <- factor(c("A", "B", "B")) # Create two factor objects fac2 <- factor(c("A", "A", "B")) |

fac1 <- factor(c("A", "B", "B")) # Create two factor objects fac2 <- factor(c("A", "A", "B"))

If we apply the c function to these two vectors, a numeric vector is returned:

c(fac1, fac2) # Apply c function to factors # 1 2 2 1 1 2 |

c(fac1, fac2) # Apply c function to factors # 1 2 2 1 1 2

The reason for that is that Base R converts vectors automatically to a numeric vector ranging from 1 to x.

However, if we apply the combine function instead, the factor levels are preserved:

combine(fac1, fac2) # Apply combine function to factors # [1] A B B A A B # Levels: A B |

combine(fac1, fac2) # Apply combine function to factors # [1] A B B A A B # Levels: A B

This is only one of the differences between Base R and the tidyverse environment and it’s a matter of taste, which rules you prefer. Have a look at the guidelines of the tidyverse for more information.

## Video & Further Resources

Do you need more info on the content of the dplyr package? Then I can recommend to have a look at the following video of my YouTube channel.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you may want to read the related articles of my website:

In this R tutorial you learned how to **use the combine function** of the dplyr package. In case you have any additional comments or questions, please let me know in the comments section.

### Statistics Globe Newsletter