Skip to content
Related Articles

Related Articles

Improve Article

Concatenate two given factor in a single factor in R

  • Last Updated : 23 May, 2021

The factor variables in R programming can be combined to form vectors and lists. Concatenation of factor type objects is not possible like the primitive concatenation operations, since they do not preserve the “factor” class of variables, which may lead to data ambiguity. 

The class of any storage object in R programming can be fetched using sapply() method, which returns the data type or mode to which an object belongs.

Syntax: sapply(X, FUN)

Parameters:
X: A vector or an object
FUN: Function applied to each element of x

Method 1 : Using unlist() method

as.factor() method is used to specify a column vector in factor format rather than being numeric. The two input factors can be declared using the vector explicitly mapped to factors using this method. 

unlist() method in R language is used to provide conversion from a list object to vector. It simplifies to produce a vector by preserving all components, that is the class of the elements is preserved. 

Syntax:



unlist( x )

where x is a list or vector

This approach preserves the data and its corresponding levels. The data is concatenated in the order in with it is specified in the list() method. 

Example:

R




fac1 <- as.factor(c(1:5))
print ("Factor1 : ")
print (fac1)
sapply(fac1,class)
  
fac2 <- as.factor(c(8:10))
print ("Factor2 : ")
print (fac2)
sapply(fac2,class)
  
# combine into one factor
combined <- unlist(list(fac1,fac2))
print ("Combined Factor : ")
print (combined)
  
sapply(combined,class)

Output

[1] “Factor1 : “

[1] 1 2 3 4 5



Levels: 1 2 3 4 5

[1] “factor” “factor” “factor” “factor” “factor”

[1] “Factor2 : “

[1] 8  9  10

Levels: 8 9 10

[1] “factor” “factor” “factor”

[1] “Combined Factor : “

[1] 1  2  3  4  5  8  9  10

Levels: 1 2 3 4 5 8 9 10

[1] “factor” “factor” “factor” “factor” “factor” “factor” “factor” “factor”

Factor type objects belonging to different data types can also be merged together in a singular list of the factor type elements. The levels are preserved in this merge. 

Example:

R




fac1 <- as.factor(letters[1:3])
print ("Factor1 : ")
print (fac1)
sapply(fac1,class)
  
fac2 <- as.factor(c(1:4))
print ("Factor2 : ")
print (fac2)
sapply(fac2,class)
  
# combine into one factor
combined <- unlist(list(fac1,fac2))
print ("Combined Factor : ")
print (combined)
  
sapply(combined,class)

Output

[1] “Factor1 : “

[1] a b c

Levels: a b c

[1] “factor” “factor” “factor”

[1] “Factor2 : “

[1] 1 2 3 4



Levels: 1 2 3 4

[1] “factor” “factor” “factor” “factor”

[1] “Combined Factor : “

[1] a b c 1 2 3 4

Levels: a b c 1 2 3 4

[1] “factor” “factor” “factor” “factor” “factor” “factor” “factor”

Method 2 : Using factor() method

The levels() method of the specific factor vector can be used to extract the individual levels in the form of independent strings. It returns the number of elements equivalent to the length of the vector. 

Syntax:

levels (fac-vec)[fac-vec]

Since, these levels are each returned as strings, therefore they belong to the same data type. Hence, they are combined now to form an atomic vector using the c() method. 

Syntax:

c (vec1 , vec2)

 where vec1 and vec2 are vectors belonging to same data type 

Example:

R




fac1 <- factor(letters[1:3])
print ("Factor1 : ")
print (fac1)
sapply(fac1,class)
  
fac2 <- factor(c(1:4))
print ("Factor2 : ")
print (fac2)
sapply(fac2,class)
  
# extracting levels of factor1
level1 <- levels(fac1)[fac1]
  
# extracting levels of factor2
level2 <- levels(fac2)[fac2]
  
# combine into one factor
combined <- factor(c( level1,level2 ))
print ("Combined Factor : ")
print (combined)
  
sapply(combined,class)

Output

[1] “Factor1 : “

[1] a b c

Levels: a b c

[1] “factor” “factor” “factor”



[1] “Factor2 : “

[1] 1 2 3 4

Levels: 1 2 3 4

[1] “factor” “factor” “factor” “factor”

[1] “Combined Factor : “

[1] a b c 1 2 3 4

Levels: 1 2 3 4 a b c

[1] “factor” “factor” “factor” “factor” “factor” “factor” “factor”




My Personal Notes arrow_drop_up
Recommended Articles
Page :