Concatenating Objects in R Programming – combine() Function
Last Updated :
31 Aug, 2020
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.
install.packages ( "dplyr" )
library (dplyr)
x <- c (1, 2, 3)
y <- c (4, 5, 6)
cat ( "Using c() function:\n" )
c (x, y)
cat ( "\n" )
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" ))
cat ( "Using c() function:\n" )
c (x, y)
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...