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"
Similar Reads
How to set level of a factor column in R dataframe to NA?
A data frame may contain columns belonging to different classes. A factor type column is a set of categorical variables each mapped to a unique level. The levels give us idea about the co-morbidity of the data variable with each other. These levels can be modified both quantitatively (increase/decre
5 min read
How to create a DataFrame from given vectors in R ?
In this article we will see how to create a Dataframe from four given vectors in R. To create a data frame in R using the vector, we must first have a series of vectors containing data. The data.frame() function is used to create a data frame from vector in R. Syntax: data.frame(vectors) Example 1.
2 min read
How to Convert Character to Factor in R?
The as.factor() method in R Programming Language is used to convert the character vector to factor class. Converting Character Vector To Factor Syntax: as.factor(char-vec) where char-vec is the character vector The class indicative of the data type of the vector can be obtained using the class() me
2 min read
How to Convert Factor to Character in R?
In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language. Method 1: Convert a Single Factor Vector to Character Vector To convert a single factor vector to a character vector we use the as.character() function of the R Language and pass th
4 min read
Get All Factor Levels of DataFrame Column in R
The data frame columns in R can be factorized on the basis of its factor columns. The data frame factor columns are composed of factor levels. Factors are used to represent categorical data. Each of the factor is denoted by a level, computed in the lexicographic order of appearance of characters or
3 min read
How to count values per level in a factor in R
In this article, we will discuss how to count the values per level in a given factor in R Programming Language. Method 1 : Using summary() method summary() method in base R is a generic function used to produce result summaries of the results of the functions computed based on the class of the argum
5 min read
Replace contents of factor column in R dataframe
In this article, we are going to see how to replace the content of the factor column in the dataframe in R Programming Language. Example 1: Replacing content of factor column Initially, the factor column is converted to a character column using the explicit conversion of as.character() method in R.
2 min read
How to convert factor levels to list in R ?
In this article, we are going to discuss how to convert the factor levels to list data structure in R Programming Language. We can get the levels of the vector using factor() function Syntax: factor(vector) Return type: vector elements with levels. If we want to get only levels, Then we can use leve
2 min read
Concatenate Vector of Character Strings in R
In this article, we will discuss how to concatenate the strings present in two or more vectors in R Programming Language. Discussed below are various methods of doing so. Method 1: Using paste() paste() function is used to combine strings present in vectors passed to it  an argument. Syntax: paste(
4 min read
Element-wise concatenation of string vector in R
Element wise concatenation of two vectors means concurrently taking values from two vectors and joining or concatenating them into one. For this paste() function is used in the R programming language. Syntax: paste(vector1,vector2,....,vectorn) Where, vectors are the inputs to paste function Example
2 min read