How to subset a named vector based on names in R?


To subset a named vector based on names, we can follow the below steps −

  • Create a named vector.
  • Subset the vector using grepl.

Create the named vector

Let’s create a name vector as shown below −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

 N F A B T B R T X E T H P
0.96 0.96 -0.57 -1.48 -1.27 1.47 -1.36 -0.48 1.78 0.49 1.19 0.94 -1.26
B X Y O C V U E A F H M T
0.89 2.60 -1.76 0.72 -0.77 0.59 -0.24 -0.79 -0.43 0.86 -0.43 1.90 0.91
I N K H N A E E M X D K E
-0.52 1.28 0.39 1.21 -0.16 -1.15 -1.96 -0.09 1.26 -0.54 0.35 -0.11 0.09
X K B R V K Z B I J M Q C
1.28 0.07 -0.22 2.25 0.31 -0.45 0.31 1.22 0.80 -0.65 0.49 -1.14 -0.95
P N H V E W R E L L X Q P
-0.12 0.76 -0.69 -0.19 1.29 -0.57 -0.67 -0.30 0.04 -0.18 0.19 -1.22 -0.81
Y E Z G G Z Q E C A Y J M
-0.41 -1.39 0.14 -0.06 -0.17 -1.57 -1.64 1.20 -1.21 -0.27 0.85 -0.62 -0.27
L F K A X M N H V E N W B
1.09 0.07 -0.70 0.64 -0.61 0.38 -0.77 0.34 0.22 0.23 0.96 -0.85 -0.94
X U F U E M X R U B V D Z
2.16 -0.79 1.83 -0.09 1.01 0.77 2.29 0.12 1.71 -0.51 -0.85 1.15 -0.32
D V O I I A B Q N I U F B
-1.85 -0.74 -2.21 1.16 0.09 0.03 -0.39 0.50 2.51 1.33 -1.02 -0.36 0.27
F A C Y W G L N A P J J K
1.10 1.04 -0.90 1.29 0.52 -0.22 -1.03 -0.84 -0.24 1.75 -1.38 -0.64 -0.69
E D M P G O A J Z B C S O
-1.22 -1.02 1.31 -1.61 -0.71 -0.38 0.15 -0.62 -1.50 -0.46 1.08 -1.45 -0.89
Z O J X W F W
-0.44 0.33 -0.13 0.41 -0.93 2.82 1.05

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with A −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^A",names(V))]

Output

  A      A    A     A    A    A    A    A    A
-0.57 -0.43 -1.15 -0.27 0.64 0.03 1.04 -0.24 0.15

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with B −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^B",names(V))]

Output

   B    B    B    B    B     B    B    B    B    B
-1.48 1.47 0.89 -0.22 1.22 -0.94 -0.51 -0.39 0.27 -0.46

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with C −

Example

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^C",names(V))]

Output

   C    C      C    C    C
-0.77 -0.95 -1.21 -0.90 1.08

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with D −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^D",names(V))]

Output

  D    D    D    D
0.35 1.15 -1.85 -1.02

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with E −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^E",names(V))]

Output

 E     E      E    E    E    E      E     E    E    E    E    E
0.49 -0.79 -1.96 -0.09 0.09 1.29 -0.30 -1.39 1.20 0.23 1.01 -1.22

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with F −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^F",names(V))]

Output

  F    F    F    F    F    F    F
0.96 0.86 0.07 1.83 -0.36 1.10 2.82

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with G −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^G",names(V))]

Output

   G    G    G    G
-0.06 -0.17 -0.22 -0.71

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with H −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^H",names(V))]

Output

 H      H    H    H    H
0.94 -0.43 1.21 -0.69 0.34

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with I −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^I",names(V))]

Output

   I   I    I    I    I
-0.52 0.80 1.16 0.09 1.33

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with J −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^J",names(V))]

Output

   J    J      J    J    J    J
-0.65 -0.62 -1.38 -0.64 -0.62 -0.13

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with K −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^K",names(V))]

Output

  K    K     K    K      K     K
0.39 -0.11 0.07 -0.45 -0.70 -0.69

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with L −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^L",names(V))]

Output

  L    L    L    L
0.04 -0.18 1.09 -1.03

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with M −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^M",names(V))]

Output

  M    M    M    M    M    M    M
1.90 1.26 0.49 -0.27 0.38 0.77 1.31

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with N −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^N",names(V))]

Output

  N    N    N    N      N    N    N    N
0.96 1.28 -0.16 0.76 -0.77 0.96 2.51 -0.84

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with O −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^O",names(V))]

Output

  O    O    O      O    O
0.72 -2.21 -0.38 -0.89 0.33

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with P −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^P",names(V))]

Output

   P    P    P      P    P
-1.26 -0.12 -0.81 1.75 -1.61

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with Q −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^Q",names(V))]

Output

   Q    Q    Q    Q
-1.14 -1.22 -1.64 0.50

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with R −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^R",names(V))]

Output

   R    R    R    R
-1.36 2.25 -0.67 0.12

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with S −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^S",names(V))]

Output

  S
-1.45

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with T −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^T",names(V))]

Output

   T    T    T    T
-1.27 -0.48 1.19 0.91

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with U −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^U",names(V))]

Output

   U    U    U      U    U
-0.24 -0.79 -0.09 1.71 -1.02

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with V −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^V",names(V))]

Output

  V    V    V    V      V    V
0.59 0.31 -0.19 0.22 -0.85 -0.74

Subset the vector V based on the names of element

Use grep function to subset vector values starting with W −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^W",names(V))]

Output

   W    W    W    W    W
-0.57 -0.85 0.52 -0.93 1.05

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with X −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^X",names(V))]

Output

  X    X    X    X    X    X    X    X    X
1.78 2.60 -0.54 1.28 0.19 -0.61 2.16 2.29 0.41

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with Y −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^Y",names(V))]

Output

   Y    Y    Y    Y
-1.76 -0.41 0.85 1.29

Subset the vector V based on the names of element

Use grepl function to subset vector values starting with Z −

 Live Demo

V<-round(rnorm(150),2)
names(V)<-sample(LETTERS[1:26],150,replace=TRUE)
V[grepl("^Z",names(V))]

Output

  Z    Z    Z    Z       Z    Z
0.31 0.14 -1.57 -0.32 -1.50 -0.44

Updated on: 13-Aug-2021

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements