如何在R中将向量的名称与另一个向量中具有相同值的字符串向量值按顺序匹配?


如果我们想将向量的名称与另一个向量中具有相同值的字符串向量值按顺序匹配,则可以使用pmatch函数。pmatch函数表示模式匹配,因此它匹配所有对应的值并返回这些值的索引。查看下面的示例以了解其工作原理。

示例

 在线演示

x1<-c("A","B","C")
x1

输出

[1] "A" "B" "C"

示例

 在线演示

y1=structure(c(1,2,3),.Names=c("C","A","B"))
y1

输出

C A B
1 2 3

示例

pmatch(x1,names(y1))

输出

[1] 2 3 1

示例

 在线演示

x2<-sample(c("A","B","C"),80,replace=TRUE)
x2

输出

[1]  "C" "B" "B" "B" "C" "C" "B" "A" "A" "A" "A" "A" "A" "A" "A" "B" "C" "A" "B"
[20] "A" "A" "C" "C" "C" "B" "B" "C" "B" "B" "A" "A" "B" "C" "C" "B" "C" "B" "A"
[39] "C" "C" "C" "A" "A" "A" "B" "C" "C" "C" "B" "C" "B" "A" "A" "B" "C" "A" "C"
[58] "A" "A" "A" "B" "C" "B" "B" "A" "B" "C" "B" "A" "A" "A" "B" "C" "A" "C" "A"
[77] "C" "A" "B" "A"

示例

 在线演示

y2<-sample(setNames(c(1,2,3),c("B","C","A")),80,replace=TRUE)
y2

输出

C B A A B B B B A B A A B A A A A C C C B B C C C B B B C A B A C B B B C C C C
2 1 3 3 1 1 1 1 3 1 3 3 1 3 3 3 3 2 2 2 1 1 2 2 2 1 1 1 2 3 1 3 2 1 1 1 2 2 2 2
B C A B B B B B C C C B B A C C A A A A B B C B A A A A B C C A A B B C B A C C
1 2 3 1 1 1 1 1 2 2 2 1 1 3 2 2 3 3 3 3 1 1 2 1 3 3 3 3 1 2 2 3 3 1 1 2 1 3 2 2

示例

pmatch(x2,names(y2))

输出

[1] 1 2 5 6 18 19 7 3 4 9 11 12 14 15 16 8 20 17 10 30 32 23 24 25 13
[26] 21 29 22 26 43 54 27 33 37 28 38 31 57 39 40 42 58 59 60 34 49 50 51 35 55
[51] 36 65 66 41 56 67 63 68 72 73 44 70 45 46 78 47 71 48 NA NA NA 52 76 NA 79
[76] NA 80 NA 53 NA

示例

 在线演示

x3<-sample(c("India","China","Bangladesh","Russia"),80,replace=TRUE)
x3

输出

[1]  "China" "India" "Bangladesh" "Bangladesh" "Bangladesh"
[6]  "India" "China" "India" "Russia" "Bangladesh"
[11] "Bangladesh" "China" "Bangladesh" "China" "Bangladesh"
[16] "Russia" "Russia" "India" "India" "China"
[21] "China" "Bangladesh" "Bangladesh" "Russia" "Russia"
[26] "Bangladesh" "Russia" "Bangladesh" "China" "India"
[31] "India" "Russia" "India" "Russia" "India"
[36] "Bangladesh" "Russia" "Russia" "China" "India"
[41] "Russia" "Bangladesh" "Bangladesh" "India" "Bangladesh"
[46] "India" "India" "India" "India" "Russia"
[51] "Bangladesh" "India" "India" "Russia" "Bangladesh"
[56] "India" "India" "India" "China" "Bangladesh"
[61] "India" "Russia" "China" "Russia" "India"
[66] "India" "Russia" "India" "China" "Russia"
[71] "China" "Russia" "Bangladesh" "Bangladesh" "China"
[76] "Russia" "India" "Russia" "China" "India"

示例

 在线演示

y3<-
sample(setNames(c(25,24,32,27),c("Russia","China","India","Bangladesh")),80,replace= TRUE)
y3

输出

Bangladesh India Russia China Bangladesh India China
   27       32      25    24       27       32    24
India Bangladesh China China India Russia Russia
  32       27       24    24    32    25    25
China China Bangladesh Russia Russia China China
   24    24    27       25       25    24    24
Russia Russia Russia India Russia China China
   25    25    25       32    25    24    24
Russia Bangladesh Bangladesh Bangladesh Russia India China
   25       27       27          27       25    32    24
Bangladesh Russia India Russia India Russia India
   27       25       32    25    32    25    32
Russia Russia Bangladesh India China China China
   25    25    27          32    24    24    24
India India Russia Russia Bangladesh China Bangladesh
   32    32 25 25 27 24 27
China India Bangladesh Bangladesh China India Russia
   24    32       27       27       24    32    25
China Bangladesh India Bangladesh India Russia China
   24       27     32    27          32    25    24
Russia Bangladesh Russia Russia Russia Russia Russia
   25       27       25    25       25    25    25
China Bangladesh India
   24    27       32

示例

pmatch(x3,names(y3))

输出

[1] 4 2 1 5 9 6 7 8 3 17 30 10 31 11 32 13 14 12 25 15 16 36 45 18 19
[26] 54 22 56 20 34 38 23 40 24 42 59 26 29 21 46 33 60 65 50 67 51 58 62 66 37
[51] 72 68 80 39 79 NA NA NA 27 NA NA 41 28 43 NA NA 44 NA 35 52 47 53 NA NA 48
[76] 63 NA 69 49 NA

示例

 在线演示

x4<-sample(c("80 degree","90 degree","100 degree"),80,replace=TRUE)
x4

输出

[1] "80 degree" "100 degree" "90 degree" "80 degree" "80 degree"
[6] "80 degree" "100 degree" "100 degree" "80 degree" "90 degree"
[11] "80 degree" "90 degree" "80 degree" "80 degree" "80 degree"
[16] "100 degree" "100 degree" "80 degree" "90 degree" "100 degree"
[21] "90 degree" "90 degree" "80 degree" "90 degree" "80 degree"
[26] "80 degree" "90 degree" "80 degree" "90 degree" "100 degree"
[31] "80 degree" "90 degree" "90 degree" "100 degree" "100 degree"
[36] "90 degree" "100 degree" "90 degree" "90 degree" "100 degree"
[41] "90 degree" "100 degree" "80 degree" "80 degree" "90 degree"
[46] "100 degree" "100 degree" "100 degree" "90 degree" "100 degree"
[51] "80 degree" "100 degree" "80 degree" "80 degree" "90 degree"
[56] "100 degree" "80 degree" "80 degree" "100 degree" "80 degree"
[61] "80 degree" "80 degree" "80 degree" "80 degree" "100 degree"
[66] "100 degree" "100 degree" "80 degree" "90 degree" "80 degree"
[71] "80 degree" "90 degree" "80 degree" "80 degree" "80 degree"
[76] "90 degree" "100 degree" "80 degree" "80 degree" "80 degree"

示例

 在线演示

<y4<-sample(setNames(c(90,100,80),c("90 degree","100 degree","80
degree")),80,replace=TRUE)
y4

输出

90 degree 100 degree 80 degree 100 degree 90 degree 90 degree 90 degree
90 100 80 100 90 90 90
100 degree 80 degree 80 degree 90 degree 90 degree 90 degree 100 degree
100 80 80 90 90 90 100
80 degree 80 degree 80 degree 100 degree 100 degree 100 degree 100 degree
80 80 80 100 100 100 100
100 degree 100 degree 100 degree 80 degree 80 degree 100 degree 100 degree
100 100 100 80 80 100 100
80 degree 80 degree 100 degree 100 degree 90 degree 90 degree 100 degree
80 80 100 100 90 90 100
80 degree 90 degree 100 degree 80 degree 100 degree 80 degree 100 degree
80 90 100 80 100 80 100
90 degree 90 degree 90 degree 100 degree 90 degree 100 degree 80 degree
90 90 90 100 90 100 80
80 degree 90 degree 90 degree 90 degree 100 degree 80 degree 80 degree
80 90 90 90 100 80 80
90 degree 100 degree 100 degree 90 degree 80 degree 100 degree 80 degree
90 100 100 90 80 100 80
80 degree 90 degree 100 degree 100 degree 100 degree 100 degree 100 degree
80 90 100 100 100 100 100
80 degree 100 degree 80 degree 80 degree 100 degree 100 degree 80 degree
80 100 80 80 100 100 80
90 degree 90 degree 100 degree
90 90 100

示例

pmatch(x4,names(y4))

输出

[1] 3 2 1 9 10 15 4 8 16 5 17 6 25 26 29 14 18 30 7 19 11 12 36 13 39
[26] 41 33 49 34 20 50 37 43 21 22 44 23 45 47 24 51 27 55 56 52 28 31 32 53 35
[51] 61 38 63 64 57 40 71 73 42 74 77 NA NA NA 46 48 54 NA 60 NA NA 65 NA NA NA
[76] 78 58 NA NA NA

更新于: 2020年12月5日

129 次查看

开启您的职业生涯

完成课程获得认证

开始学习
广告