- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to combine two factor vectors to create one in R?
To combine two factor vectors, we can extract the unique levels of both the vectors then combine those levels. This can be done by using unique function. Also, we can set the levels of the original vectors to the combination of the levels, in this way, we can complete both the vectors with missing levels. Check out the examples below to understand how it works.
Example1
x1<−factor(LETTERS[1:5]) y1<−factor(LETTERS[1:7]) factor_levels1<−unique(c(levels(x1),levels(y1))) x1<−factor(x1,levels=factor_levels1) x1 [1] A B C D E Levels: A B C D E F G y1<−factor(y1,levels=factor_levels1) y1 [1] A B C D E F G Levels: A B C D E F G
Example2
x2<−factor(LETTERS[1:26]) x2 [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z y2<−factor(LETTERS[11:26]) y2 [1] K L M N O P Q R S T U V W X Y Z Levels: K L M N O P Q R S T U V W X Y Z factor_levels2<−unique(c(levels(x2),levels(y2))) x2<−factor(x2,levels=factor_levels2) x2 [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z y2<−factor(y2,levels=factor_levels2) y2 [1] K L M N O P Q R S T U V W X Y Z Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Example3
x3<−factor(sample(c("India","China","Russia"),50,replace=TRUE)) x3 [1] Russia China India Russia India India India India China Russia [11] China China China India India Russia India China Russia Russia [21] China Russia Russia China India China India India China India [31] India India India Russia India India Russia Russia China Russia [41] Russia Russia India China Russia China India Russia China India Levels: China India Russia y3<−factor(sample(c("India","China","Russia","Indonesia","Croatia"),50,replace=TRUE)) y3 [1] Croatia China India Russia China India Indonesia [8] China Indonesia Indonesia Russia India Indonesia Russia [15] Croatia Croatia India Croatia Russia Russia India [22] India Croatia Russia India China Croatia Russia [29] India Russia China China Russia Russia India [36] India India China Indonesia Indonesia Indonesia Indonesia [43] China Croatia China Croatia Croatia India Indonesia [50] Croatia Levels: China Croatia India Indonesia Russia factor_levels3<−unique(c(levels(x3),levels(y3))) factor_levels3 [1] "China" "India" "Russia" "Croatia" "Indonesia" x3<−factor(x3,levels=factor_levels3) x3 [1] Russia China India Russia India India India India China Russia [11] China China China India India Russia India China Russia Russia [21] China Russia Russia China India China India India China India [31] India India India Russia India India Russia Russia China Russia [41] Russia Russia India China Russia China India Russia China India Levels: China India Russia Croatia Indonesia y3<−factor(y3,levels=factor_levels3) y3 [1] Croatia China India Russia China India Indonesia [8] China Indonesia Indonesia Russia India Indonesia Russia [15] Croatia Croatia India Croatia Russia Russia India [22] India Croatia Russia India China Croatia Russia [29] India Russia China China Russia Russia India [36] India India China Indonesia Indonesia Indonesia Indonesia [43] China Croatia China Croatia Croatia India Indonesia [50] Croatia Levels: China India Russia Croatia Indonesia
Example4
x4<−factor(sample(c("I","II"),50,replace=TRUE)) x4 [1] II I II I I I I II II II II I I II II II I I II I I I I II II [26] II II II I II I I II I I I I II I I II I II I II I I I I I Levels: I II y4<−factor(sample(c("III","IV","V"),50,replace=TRUE)) y4 [1] V V V IV V III V V IV V III V IV III III III V V V [20] V V IV III III V III III IV IV III III V V V III V V III [39] IV III V IV III IV IV V IV IV V IV Levels: III IV V factor_levels4<−unique(c(levels(x4),levels(y4))) factor_levels4 [1] "I" "II" "III" "IV" "V" x4<−factor(x4,levels=factor_levels4) x4 [1] II I II I I I I II II II II I I II II II I I II I I I I II II [26] II II II I II I I II I I I I II I I II I II I II I I I I I Levels: I II III IV V y4<−factor(y4,levels=factor_levels4) y4 [1] V V V IV V III V V IV V III V IV III III III V V V [20] V V IV III III V III III IV IV III III V V V III V V III [39] IV III V IV III IV IV V IV IV V IV Levels: I II III IV V
- Related Articles
- How to combine array of vectors in R?
- How to combine two vectors by separating with different special characters in R?
- How to combine two matrices to create a block-diagonal matrix in R?
- How to create combinations for each string values in two vectors in R?
- How to create boxplots based on two factor data in R?
- How to create combination of multiple vectors in R?
- How to combine two vectors while replacing the NA values with the values in the other vector in R?
- How to concatenate two or more vectors in R?
- How to create table of two factor columns in an R data frame?
- How to create a sequence of values between two vectors in R using corresponding elements?
- How to multiply two vectors in R as in mathematics?
- How to find the union of two vectors in R?
- How to find the covariance between two vectors in R?
- How to create boxplot of vectors having different lengths in R?
- How to create a matrix with vectors as elements in R?

Advertisements