为了合并两个因子向量,我们可以提取两个向量的唯一水平,然后合并这些水平。这可以通过使用独特的功能来完成。另外,我们可以将原始向量的水平设置为这些水平的组合,这样,我们就可以完成两个缺失水平的向量。查看以下示例以了解其工作原理。
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
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
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
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