如何在 R 中找到另一个向量中存在的频率向量元素?
如果一个向量值存在于另一个向量中,那么我们可能希望找到该值在另一个向量中的频率/计数。例如,如果我们有两个向量,例如 x 和 y,并且 y 中的一些值也存在于 x 中。因此,我们可以使用命令 colSums(outer(x,y,"==")) 找到 x 中 y 值的频率。
示例
x1<-LETTERS[1:10] x1
输出
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
示例
y1<-LETTERS[5:15] y1
输出
[1] "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O"
示例
colSums(outer(x1,y1,"=="))
输出
[1] 1 1 1 1 1 1 0 0 0 0 0
示例
x2<-sample(LETTERS[1:10],100,replace=TRUE) x2
输出
[1] "D" "D" "E" "J" "H" "J" "E" "G" "B" "B" "G" "D" "E" "D" "F" "G" "B" "H" [19] "I" "E" "G" "H" "I" "I" "B" "B" "I" "C" "H" "F" "D" "D" "B" "B" "B" "J" [37] "C" "A" "F" "H" "A" "A" "G" "I" "F" "J" "D" "H" "H" "A" "I" "A" "G" "D" [55] "C" "H" "E" "A" "J" "D" "H" "A" "A" "F" "E" "A" "G" "G" "I" "C" "A" "D" [73] "E" "F" "J" "A" "B" "A" "J" "A" "A" "A" "F" "E" "C" "H" "I" "G" "A" "E" [91] "E" "D" "J" "D" "I" "H" "B" "H" "C" "E"
示例
y2<-sample(LETTERS[6:12],100,replace=TRUE) y2
输出
[1] "K" "I" "L" "L" "K" "F" "G" "F" "K" "L" "G" "G" "H" "I" "I" "K" "I" "F" [19] "G" "L" "K" "J" "L" "I" "I" "I" "F" "K" "H" "J" "I" "L" "F" "L" "L" "H" [37] "K" "H" "H" "F" "H" "J" "F" "L" "I" "I" "L" "F" "L" "F" "K" "K" "F" "F" [55] "J" "H" "I" "F" "K" "J" "F" "J" "I" "I" "F" "I" "G" "F" "I" "G" "H" "K" [73] "G" "I" "K" "H" "F" "L" "F" "H" "L" "I" "H" "G" "F" "G" "I" "G" "L" "F" [91] "I" "F" "H" "J" "J" "F" "K" "J" "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0 2 2 2 0 2 2 2 0 2 0 0 0 [149] 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 0 0 0 2 0 0 2 0 0 0 0 0 2 0 0 2 [186] 0 2 0 2 2 2 2 0 2 2 2 2 0 2 0
示例
colSums(outer(x3,y3,"=="))
输出
[1] 48 48 48 47 47 48 47 47 48 47 48 47 48 47 47 48 47 48 47 48 48 47 47 47 48 [26] 48 47 48 48 48 47 48 48 48 48 47 48 47 48 47 48 48 48 47 47 48 48 47 47 47 [51] 48 47 48 48 48 48 48 48 48 48 48 48 48 47 48 47 47 47 48 48 48 48 48 47 47 [76] 48 47 48 48 47 48 48 47 48 48 47 47 48 47 47 48 48 47 48 48 47 48 48 48 47 [101] 47 48 48 47 47 47 48 48 48 47 48 47 48 47 48 48 48 48 48 47 47 47 47 48 48 [126] 48 48 48 48 48 48 47 48 48 48 48 47 47 47 48 47 47 47 48 47 48 48 48 48 48 [151] 48 48 47 48 48 48 48 48 48 48 48 48 48 48 48 48 47 48 47 48 48 48 47 48 48 [176] 47 48 48 48 48 48 47 48 48 47 48 47 48 47 47 47 47 48 47 47 47 47 48 47 48
示例
x4<-sample(rnorm(5),100,replace=TRUE) x4
输出
[1] -0.2818766 0.7100732 0.1288669 1.4954190 0.7100732 1.4954190 [7] 1.4954190 1.1059633 -0.2818766 0.7100732 -0.2818766 1.1059633 [13] -0.2818766 0.1288669 0.1288669 0.7100732 0.7100732 0.1288669 [19] 0.1288669 0.1288669 1.4954190 0.7100732 -0.2818766 1.4954190 [25] 1.1059633 0.7100732 1.1059633 1.1059633 1.1059633 -0.2818766 [31] 1.1059633 -0.2818766 1.4954190 1.1059633 1.1059633 1.4954190 [37] 1.1059633 0.1288669 1.4954190 1.1059633 0.7100732 0.7100732 [43] 1.1059633 1.1059633 1.4954190 1.4954190 0.7100732 0.1288669 [49] 1.4954190 1.1059633 1.4954190 1.4954190 0.1288669 1.4954190 [55] -0.2818766 0.7100732 -0.2818766 1.1059633 0.7100732 1.1059633 [61] -0.2818766 0.1288669 0.7100732 -0.2818766 -0.2818766 -0.2818766 [67] 0.1288669 1.4954190 0.1288669 1.1059633 0.7100732 0.7100732 [73] 1.4954190 0.1288669 -0.2818766 -0.2818766 -0.2818766 0.7100732 [79] -0.2818766 1.1059633 -0.2818766 -0.2818766 1.4954190 1.4954190 [85] 0.1288669 0.7100732 -0.2818766 0.7100732 1.4954190 0.7100732 [91] -0.2818766 -0.2818766 0.7100732 0.7100732 0.7100732 0.7100732 [97] 1.4954190 -0.2818766 0.7100732 -0.2818766
示例
y4<-sample(rnorm(2),100,replace=TRUE) y4
输出
[1] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -1.0352421 -1.0352421 [7] -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814 -0.4483814 [13] -1.0352421 -0.4483814 -1.0352421 -0.4483814 -1.0352421 -1.0352421 [19] -1.0352421 -1.0352421 -1.0352421 -0.4483814 -1.0352421 -1.0352421 [25] -0.4483814 -1.0352421 -1.0352421 -0.4483814 -0.4483814 -0.4483814 [31] -0.4483814 -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814 [37] -0.4483814 -1.0352421 -1.0352421 -1.0352421 -0.4483814 -0.4483814 [43] -0.4483814 -0.4483814 -0.4483814 -0.4483814 -1.0352421 -0.4483814 [49] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -1.0352421 -0.4483814 [55] -1.0352421 -1.0352421 -1.0352421 -1.0352421 -1.0352421 -1.0352421 [61] -1.0352421 -0.4483814 -1.0352421 -0.4483814 -1.0352421 -0.4483814 [67] -0.4483814 -1.0352421 -0.4483814 -0.4483814 -0.4483814 -0.4483814 [73] -0.4483814 -1.0352421 -0.4483814 -0.4483814 -1.0352421 -0.4483814 [79] -0.4483814 -0.4483814 -1.0352421 -1.0352421 -0.4483814 -0.4483814 [85] -0.4483814 -0.4483814 -1.0352421 -0.4483814 -0.4483814 -1.0352421 [91] -0.4483814 -0.4483814 -0.4483814 -1.0352421 -0.4483814 -0.4483814 [97] -0.4483814 -1.0352421 -0.4483814 -0.4483814
示例
colSums(outer(x4,y4,"=="))
输出
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
示例
x5<-sample(round(rnorm(5),0),100,replace=TRUE) x5
输出
[1] 0 0 0 0 1 0 0 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 [38] 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1 0 1 1 1 0 0 1 0 1 1 0 1 1 0 0 [75] 0 0 1 0 1 0 1 0 0 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1
示例
y5<-sample(round(rnorm(5,2,1),0),100,replace=TRUE) y5
输出
[1] 0 2 1 3 2 3 2 0 3 3 3 2 1 2 0 0 0 2 2 2 2 3 0 2 0 2 3 1 2 2 2 0 2 2 2 0 3 [38] 1 2 0 2 1 2 2 3 2 1 1 1 2 3 2 2 3 0 2 0 2 3 2 3 0 0 2 0 0 0 0 0 3 2 2 2 3 [75] 2 0 1 0 2 1 3 1 2 0 0 1 2 0 2 2 0 1 0 2 1 2 2 2 0 2
示例
colSums(outer(x5,y5,"=="))
输出
[1] 50 0 50 0 0 0 0 50 0 0 0 0 50 0 50 50 50 0 0 0 0 0 50 0 50 [26] 0 0 50 0 0 0 50 0 0 0 50 0 50 0 50 0 50 0 0 0 0 50 50 50 0 [51] 0 0 0 0 50 0 50 0 0 0 0 50 50 0 50 50 50 50 50 0 0 0 0 0 0 [76] 50 50 50 0 50 0 50 0 50 50 50 0 50 0 0 50 50 50 0 50 0 0 0 50 0
广告