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