- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract the names of vector values from a named vector in R?
How to extract the names of vector values from a named vector in R?
The names of vector values are created by using name function and the names can be extracted by using the same function. For example, if we have a vector called x that contains five values(1 to 5) and their names are defined as first, second, third, fourth and fifth then the names of values in x can be extracted by using names(x)[x==1].
Example1
> x1<-1:4 > names(x1)<-c("one","two","three","four") > x1
Output
one two three four 1 2 3 4
Example
> names(x1)[x1==1]
Output
[1] "one"
Example
> names(x1)[x1==2]
Output
[1] "two"
Example
> names(x1)[x1==3]
Output
[1] "three"
Example
> names(x1)[x1==4]
Output
[1] "four"
Example2
> x2<-sample(0:5,120,replace=TRUE) > x2
Output
[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
Example
> names(x2)<-sample(c("India","Russia","Turkey","France","Germany"),120,replace=TRUE) > x2
Output
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
Example
> names(x2)[x2==0]
Output
[1] "Germany" "Russia" "Russia" "Turkey" "France" "France" "India" [8] "France" "Germany" "France" "Germany" "Russia" "Germany"
Example
> names(x2)[x2==1]
Output
[1] "Russia" "Germany" "Germany" "France" "France" "Russia" "France" [8] "France" "France" "Turkey" "Russia" "Turkey" "Russia" "France" [15] "Germany"
Example
> names(x2)[x2==2]
Output
[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"
Example
> names(x2)[x2==3]
Output
[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"
Example
> names(x2)[x2==4]
Output
[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"
Example
> names(x2)[x2==5]
Output
[1] "Russia" "France" "Germany" "Russia" "India" "France" "India" [8] "Turkey" "Germany" "Russia" "India" "Russia" "France" "Turkey" [15] "India" "Germany" "India" "India" "Germany" "India" "Russia"
Example3
> x3<-sample(LETTERS[1:26],100,replace=TRUE) > x3
Output
[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"
Example
> names(x3)<-sample(c("C1","C2","C3","C4","C5"),100,replace=TRUE) > x3
Output
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"
Example
> names(x3)[x3=="A"]
Output
[1] "C2" "C2"
Example
> names(x3)[x3=="B"]
Output
[1] "C4" "C4" "C4" "C1"
Example
> names(x3)[x3=="C"]
Output
[1] "C1" "C3" "C1" "C3" "C2" "C1" "C1" "C3"
Example
> names(x3)[x3=="D"]
Output
[1] "C1" "C1" "C5"
Example
> names(x3)[x3=="E"]
Output
[1] "C4" "C5" "C3" "C4" "C1"
Example
> names(x3)[x3=="T"]
Output
[1] "C3" "C1" "C5"
Example
> names(x3)[x3=="U"]
Output
[1] "C4" "C3" "C1"
Example
> names(x3)[x3=="W"]
Output
[1] "C2" "C3" "C4" "C2" "C2" "C1" "C4"
Example
> names(x3)[x3=="X"]
Output
[1] "C1" "C2" "C1" "C1"
Example
> names(x3)[x3=="Y"]
Output
[1] "C3" "C2" "C5"
Example
> names(x3)[x3=="Z"]
Output
[1] "C4" "C3"
- Related Articles
- How to remove names from a named vector in R?
- How to extract the maximum value from named vector in R?
- How to subset a named vector based on names in R?
- How to match the names of a vector in sequence with string vector values in another vector having same values in R?
- How to extract words from a string vector in R?
- How to extract all string values from a vector in R with maximum lengths?
- How to extract string before slash from a vector in R?
- Extract a data frame column values as a vector by matching a vector in R.
- How to convert a named vector to a list in R?
- R programming to subtract all values in a vector from all values in another vector.
- How to replace values in a vector with values in the same vector in R?
- How to extract a data.table row as a vector in R?
- How to extract strings based on first character from a vector of strings in R?
- How to determine the percentiles of a vector values in R?
- How to convert two columns of an R data frame to a named vector?

Advertisements