How to combine two factor vectors to create one in R?

R ProgrammingServer Side ProgrammingProgramming

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

 Live Demo

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

 Live Demo

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

 Live Demo

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
raja
Published on 06-Nov-2020 14:13:20
Advertisements