How to get row index based on a value of an R data frame column?


A row of an R data frame can have multiple ways in columns and these values can be numerical, logical, string etc. It is easy to find the values based on row numbers but finding the row numbers based on a value is different. If we want to find the row number for a particular value in a specific column then we can extract the whole row which seems to be a better way and it can be done by using single square brackets to take the subset of the row.

Example

Consider the below data frame −

 Live Demo

x1<-rnorm(20,0.5)
x2<-rpois(20,2)
x3<-rpois(20,5)
x4<-rpois(20,10)
x5<-rnorm(20,2)
df<-data.frame(x1,x2,x3,x4,x5)
df

Output

      x1       x2 x3  x4      x5
1 1.33780769    1 5    1    3.3322153
2 2.22651263    3 5    14   3.4969403
3 0.53231950    4 9    8    3.7816594
4 0.99704756    1 6    13    2.7308284
5 0.37735760    2 5    7     2.3210021
6 0.96967898    3 7    8     1.9086576
7 0.06543569    2 4    9    1.4005479
8 1.59064892    1 8    14    1.1250329
9 0.48771130    1 4    11    2.2479028
10 0.64134261   0 9    5    2.4194513
11 0.43355439   3 2    18    -0.7633724
12 1.31803573   3 4    10    2.6201427
13 0.25367683   3 6    9    1.5728880
14 1.07884550   5 4    9    0.8045540
15 0.78574641   6 6    8    1.3538888
16 -0.27873444  2 6    12    3.5431787
17 0.28112519   2 3    8    4.1865617
18 1.24416762   4 4    11    2.3626763
19 -0.04745463  2 5    13    3.4203297
20 1.33358241   1 9    14    2.8205837

Finding the rows that will also return row indexes for a value −

Example

df[df$x4==14,]

Output

      x1   x2 x3 x4 x5
2 2.226513  3 5  14 3.496940
8 1.590649  1 8  14 1.125033
20 1.333582 1 9  14 2.820584

Example

df[df$x4==12,]

Output

      x1     x2  x3  x4   x5
16 -0.2787344 2  6   12 3.543179

Example

df[df$x4==7,]

Output

      x1    x2  x3 x4    x5
5 0.3773576 2   5   7 2.321002

Example

df[df$x4==9,]

Output

      x1       x2 x3 x4    x5
7 0.06543569    2 4   9    1.400548
13 0.25367683   3 6   9    1.572888
14 1.07884550   5 4   9    0.804554

example

df[df$x2==2,]

Output

      x1       x2 x3 x4    x5
5 0.37735760    2 5   7    2.321002
7 0.06543569    2 4   9    1.400548
16 -0.27873444  2 6   12   3.543179
17 0.28112519   2 3   8    4.186562
19 -0.04745463  2 5   13    3.420330

Example

df[df$x2==5,]

Output

      x1    x2 x3 x4   x5
14 1.078845 5   4  9  0.804554

Example

df[df$x2==4,]

Output

      x1    x2 x3 x4   x5
3 0.5323195 4   9 8  3.781659
18 1.2441676 4  4 11 2.362676

Example

df[df$x3==4,]

Output

      x1    x2 x3  x4    x5
7 0.06543569 2 4   9    1.400548
9 0.48771130 1 4  11   2.247903
12 1.31803573 3 4 10   2.620143
14 1.07884550 5 4  9    0.804554
18 1.24416762 4 4 11   2.362676

Example

df[df$x3==6,]

Output

      x1    x2 x3 x4    x5
4 0.9970476 1   6 13  2.730828
13 0.2536768 3  6 9   1.572888
15 0.7857464 6  6 8   1.353889
16 -0.2787344 2 6 12  3.543179

Example

df[df$x3==5,]

Output

      x1    x2 x3 x4 x5
1 1.33780769 1 5  1 3.332215
2 2.22651263 3 5 14 3.496940
5 0.37735760 2 5  7 2.321002
19 -0.04745463 2 5 13 3.420330

Example

df[df$x3==8,]

Output

      x1   x2 x3 x4 x5
8 1.590649 1  8 14 1.125033

Example

df[df$x2==1,]

Output

      x1    x2 x3  x4   x5
1 1.3378077 1   5  1  3.332215
4 0.9970476 1   6  13  2.730828
8 1.5906489 1   8  14  1.125033
9 0.4877113 1   4  11  2.247903
20 1.3335824 1  9  14  2.820584

Updated on: 08-Sep-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements