Concatenating Objects in R Programming – combine() Function

In R programming, coercion function c() and combine() function are similar to each other but are different in a way. combine() functions acts like c() and unlist() functions but uses consistent dplyr coercion rules. Moreover, combine() function is used to combine factors in R programming. In this article, we’ll see the implementation of combine() and c() function with same outputs and different outputs on the same operation.

Syntax: combine(x, y, …)

Parameters:
x, y, …. are vectors to combine

Example 1:

In this example, we’ll see the same output of both these functions on same operation.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Package required
install.packages("dplyr")
  
# Load the library
library(dplyr)
  
x <- c(1, 2, 3)
y <- c(4, 5, 6)
  
# Using c() function
cat("Using c() function:\n")
c(x, y)
  
cat("\n")
# Using combine() function
cat("Using combine() function:\n")
combine(x, y)

chevron_right


Output:

Using c() function:
[1] 1 2 3 4 5 6

Using combine() function:
[1] 1 2 3 4 5 6

Example 2:
In this example, we’ll see the different outputs of both these functions on the same operation.

filter_none

edit
close

play_arrow

link
brightness_4
code

library(dplyr)
  
x <- factor(c("a", "a", "b", "c"))
y <- factor(c("b", "a", "c", "b"))
  
# Using c() function
cat("Using c() function:\n")
c(x, y)
  
# Using combine() function
cat("\n")
cat("Using combine() function:\n")
combine(x, y)

chevron_right


Output:

Using c() function:
[1] 1 1 2 3 2 1 3 2

Using combine() function:
[1] a a b c b a c b
Levels: a b c



My Personal Notes arrow_drop_up

Blockchain Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai