How to find the location of a string in an R data frame?


To find the location of a numerical value in an R data frame we use which function and if the value is string then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say tutor then we can find the location of tutor by using the command which(df=="tutor",arr.ind=TRUE).

Example1

Consider the below data frame −

Live Demo

> x1<-sample(c("2015","2018","2020"),20,replace=TRUE)
> x2<-sample(c("2015","2018","2020"),20,replace=TRUE)
> x3<-sample(c("2015","2018","2020"),20,replace=TRUE)
> df1<-data.frame(x1,x2,x3)
> df1

Output

     x1   x2   x3
1  2018 2020 2018
2  2020 2020 2015
3  2018 2020 2015
4  2018 2015 2020
5  2018 2015 2018
6  2018 2020 2015
7  2015 2018 2020
8  2020 2018 2020
9  2015 2015 2018
10 2015 2015 2020
11 2020 2020 2020
12 2018 2018 2015
13 2015 2018 2015
14 2015 2018 2018
15 2015 2020 2020
16 2015 2020 2015
17 2015 2015 2018
18 2020 2015 2015
19 2020 2015 2015
20 2015 2020 2020

Finding the location of 2020 in df1 −

> which(df1=="2020",arr.ind=TRUE)

Output

      row col
 [1,]   2   1
 [2,]   8   1
 [3,]  11   1
 [4,]  18   1
 [5,]  19   1
 [6,]   1   2
 [7,]   2   2
 [8,]   3   2
 [9,]   6   2
[10,]  11   2
[11,]  15   2
[12,]  16   2
[13,]  20   2
[14,]   4   3
[15,]   7   3
[16,]   8   3
[17,]  10   3
[18,]  11   3
[19,]  15   3
[20,]  20   3

Example2

Live Demo

> y1<-sample(c("Asia","Africa","Europe"),20,replace=TRUE)
> y2<-sample(c("Asia","Africa","Europe"),20,replace=TRUE)
> df2<-data.frame(y1,y2)
> df2

Output

       y1     y2
1  Europe   Asia
2  Europe Africa
3  Africa   Asia
4  Africa Africa
5  Europe Europe
6    Asia   Asia
7  Europe Europe
8  Africa Europe
9  Africa   Asia
10 Europe Africa
11 Europe Africa
12 Africa Europe
13 Europe Africa
14 Europe   Asia
15 Africa Europe
16 Africa   Asia
17 Europe Africa
18 Europe Africa
19   Asia   Asia
20 Africa   Asia

Finding the location of Europe in df2 −

> which(df2=="Europe",arr.ind=TRUE)

Output

      row col
 [1,]   1   1
 [2,]   2   1
 [3,]   5   1
 [4,]   7   1
 [5,]  10   1
 [6,]  11   1
 [7,]  13   1
 [8,]  14   1
 [9,]  17   1
[10,]  18   1
[11,]   5   2
[12,]   7   2
[13,]   8   2
[14,]  12   2
[15,]  15   2

Updated on: 06-Mar-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements