如何从R中的命名向量中提取向量值的名称?

如何从R中的命名向量中提取向量值的名称?

向量值的名称是通过使用名称函数创建的,并且可以使用相同的函数来提取名称。例如,如果我们有一个名为x的向量,其中包含五个值(1到5),并且它们的名称分别定义为第一,第二,第三,第四和第五,则可以使用names(x)提取x中值的名称。 [x == 1]。

例1

> x1<-1:4

> names(x1)<-c("one","two","three","four")

> x1

输出结果

one two three four

1 2 3 4

示例

> names(x1)[x1==1]

输出结果

[1] "one"

示例

> names(x1)[x1==2]

输出结果

[1] "two"

示例

> names(x1)[x1==3]

输出结果

[1] "three"

示例

> names(x1)[x1==4]

输出结果

[1] "four"

例2

> x2<-sample(0:5,120,replace=TRUE)

> x2

输出结果

 [1] 3 5 1 4 5 2 3 2 4 5 1 4 2 4 4 3 1 0 5 0 2 1 5 1 3 1 0 2 3 0 3 2 4 2 3 2 0

[38] 3 5 3 4 3 2 3 3 4 4 2 3 2 3 5 5 5 2 1 2 5 2 3 2 2 4 4 2 4 1 4 0 5 5 5 4 0

[75] 1 3 0 3 3 5 1 2 4 5 3 0 3 5 0 3 5 4 3 5 5 2 5 2 1 3 0 0 3 1 2 1 2 3 1 3 4

[112] 0 4 1 4 4 4 4 5 2

示例

> names(x2)<-sample(c("India","Russia","Turkey","France","Germany"),120,replace=TRUE)

> x2

输出结果

France Russia Russia Russia France Germany Germany France Turkey Germany

3 5 1 4 5 2 3 2 4 5

Germany India Turkey Russia Germany Turkey Germany Germany Russia Russia

1 4 2 4 4 3 1 0 5 0

Turkey France India France Germany Russia Russia Russia India Turkey

2 1 5 1 3 1 0 2 3 0

Turkey France India Germany Turkey India France India France Russia

3 2 4 2 3 2 0 3 5 3

India Turkey Germany Germany Turkey Russia Germany Turkey Germany Russia

4 3 2 3 3 4 4 2 3 2

Russia India Turkey Germany India France Russia Russia India France

3 5 5 5 2 1 2 5 2 3

France Russia India India India Turkey France India France India

2 2 4 4 2 4 1 4 0 5

Russia France India India France Turkey France Turkey Turkey Turkey

5 5 4 0 1 3 0 3 3 5

Turkey Turkey India India Russia Germany Turkey Germany France Russia

1 2 4 5 3 0 3 5 0 3

India Germany India India Germany Russia India Russia Russia Russia

5 4 3 5 5 2 5 2 1 3

Germany Russia Germany Turkey India Russia Germany Russia France India

0 0 3 1 2 1 2 3 1 3

France Germany Germany Germany Russia France India Turkey Russia Germany

4 0 4 1 4 4 4 4 5 2

示例

> names(x2)[x2==0]

输出结果

[1] "Germany" "Russia" "Russia" "Turkey" "France" "France" "India"

[8] "France" "Germany" "France" "Germany" "Russia" "Germany"

示例

> names(x2)[x2==1]

输出结果

[1] "Russia" "Germany" "Germany" "France" "France" "Russia" "France"

[8] "France" "France" "Turkey" "Russia" "Turkey" "Russia" "France"

[15] "Germany"

示例

> names(x2)[x2==2]

输出结果

[1] "Germany" "France" "Turkey" "Turkey" "Russia" "France" "Germany"

[8] "India" "Germany" "Turkey" "Russia" "India" "Russia" "India"

[15] "France" "Russia" "India" "Turkey" "Russia" "Russia" "India"

[22] "Germany" "Germany"

示例

> names(x2)[x2==3]

输出结果

[1] "France" "Germany" "Turkey" "Germany" "India" "Turkey" "Turkey"

[8] "India" "Russia" "Turkey" "Germany" "Turkey" "Germany" "Russia"

[15] "France" "Turkey" "Turkey" "Turkey" "Russia" "Turkey" "Russia"

[22] "India" "Russia" "Germany" "Russia" "India"

示例

> names(x2)[x2==4]

输出结果

[1] "Russia" "Turkey" "India" "Russia" "Germany" "India" "India"

[8] "Russia" "Germany" "India" "India" "Turkey" "India" "India"

[15] "India" "Germany" "France" "Germany" "Russia" "France" "India"

[22] "Turkey"

示例

> names(x2)[x2==5]

输出结果

[1] "Russia" "France" "Germany" "Russia" "India" "France" "India"

[8] "Turkey" "Germany" "Russia" "India" "Russia" "France" "Turkey"

[15] "India" "Germany" "India" "India" "Germany" "India" "Russia"

例子3

> x3<-sample(LETTERS[1:26],100,replace=TRUE)

> x3

输出结果

[1] "A" "U" "E" "B" "X" "S" "N" "C" "I" "W" "K" "R" "O" "G" "X" "G" "H" "C"

[19] "G" "M" "M" "U" "O" "C" "C" "N" "W" "O" "W" "C" "B" "D" "L" "P" "W" "M"

[37] "J" "L" "W" "A" "R" "M" "B" "U" "J" "B" "L" "W" "D" "Y" "J" "N" "I" "V"

[55] "J" "S" "Q" "K" "K" "K" "J" "C" "H" "E" "C" "T" "R" "X" "I" "T" "Z" "H"

[73] "E" "I" "Q" "E" "D" "C" "I" "T" "Y" "H" "K" "W" "X" "M" "Y" "E" "Q" "F"

[91] "G" "Q" "Q" "O" "K" "L" "R" "Z" "F" "K"

示例

> names(x3)<-sample(c("C1","C2","C3","C4","C5"),100,replace=TRUE)

> x3

输出结果

C2 C4 C4 C4 C1 C3 C3 C1 C5 C2 C5 C5 C4 C1 C2 C5 C1 C3 C5 C3

"A" "U" "E" "B" "X" "S" "N" "C" "I" "W" "K" "R" "O" "G" "X" "G" "H" "C" "G" "M"

C5 C3 C1 C1 C3 C4 C3 C1 C4 C2 C4 C1 C3 C3 C2 C1 C1 C3 C2 C2

"M" "U" "O" "C" "C" "N" "W" "O" "W" "C" "B" "D" "L" "P" "W" "M" "J" "L" "W" "A"

C1 C2 C4 C1 C5 C1 C5 C1 C1 C3 C3 C1 C3 C1 C3 C4 C5 C1 C3 C3

"R" "M" "B" "U" "J" "B" "L" "W" "D" "Y" "J" "N" "I" "V" "J" "S" "Q" "K" "K" "K"

C2 C1 C1 C5 C1 C3 C5 C1 C2 C1 C4 C5 C3 C1 C4 C4 C5 C3 C4 C5

"J" "C" "H" "E" "C" "T" "R" "X" "I" "T" "Z" "H" "E" "I" "Q" "E" "D" "C" "I" "T"

C2 C4 C4 C4 C1 C5 C5 C1 C3 C2 C4 C2 C4 C1 C1 C1 C2 C3 C5 C5

"Y" "H" "K" "W" "X" "M" "Y" "E" "Q" "F" "G" "Q" "Q" "O" "K" "L" "R" "Z" "F" "K"

示例

> names(x3)[x3=="A"]

输出结果

[1] "C2" "C2"

示例

> names(x3)[x3=="B"]

输出结果

[1] "C4" "C4" "C4" "C1"

示例

> names(x3)[x3=="C"]

输出结果

[1] "C1" "C3" "C1" "C3" "C2" "C1" "C1" "C3"

示例

> names(x3)[x3=="D"]

输出结果

[1] "C1" "C1" "C5"

示例

> names(x3)[x3=="E"]

输出结果

[1] "C4" "C5" "C3" "C4" "C1"

示例

> names(x3)[x3=="T"]

输出结果

[1] "C3" "C1" "C5"

示例

> names(x3)[x3=="U"]

输出结果

[1] "C4" "C3" "C1"

示例

> names(x3)[x3=="W"]

输出结果

[1] "C2" "C3" "C4" "C2" "C2" "C1" "C4"

示例

> names(x3)[x3=="X"]

输出结果

[1] "C1" "C2" "C1" "C1"

示例

> names(x3)[x3=="Y"]

输出结果

[1] "C3" "C2" "C5"

示例

> names(x3)[x3=="Z"]

输出结果

[1] "C4" "C3"

以上是 如何从R中的命名向量中提取向量值的名称? 的全部内容, 来源链接: utcz.com/z/347132.html

回到顶部