How to find the position of a data frame’s column value based on a column value of another data frame in R?

R ProgrammingServer Side ProgrammingProgramming

In data analysis, we encounter many problems with a lot of variations among them. One such problem is we have some information in a place that needs to be checked through a different place and these places can be data frames. Therefore, we need to find the position of a data frame’s column value based on a column value of another data frame. In R, we can easily do it with the help of which function.

Example

 Live Demo

Consider the below data frame −

set.seed(12121)
x1<−sample(0:2,20,replace=TRUE)
y1<−sample(0:5,20,replace=TRUE)
df1<−data.frame(x1,y1)
df1

Output

x1 y1
1 0 3
2 2 2
3 0 2
4 1 4
5 0 4
6 2 0
7 0 3
8 0 1
9 2 5
10 1 0
11 0 1
12 1 1
13 0 3
14 0 0
15 2 0
16 1 5
17 0 2
18 2 0
19 1 4
20 0 5

Example

x2<−sample(1:2,20,replace=TRUE)
y2<−sample(1:5,20,replace=TRUE)
df2<−data.frame(x2,y2)
df2

Output

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

Finding the position of x1 of df1 in x2 of df2 −

which(df1$x1 %in% df2$x2)

Output

[1] 2 4 6 9 10 12 15 16 18 19

Finding the position of y1 of df1 in y2 of df2 −

which(df1$y1 %in% df2$y2)

Output

[1] 1 2 3 4 5 7 8 9 11 12 13 16 17 19 20

Let’s have a look at another example −

x3<−sample(21:25,20,replace=TRUE)
y3<−sample(26:50,20)
df3<−data.frame(x3,y3)
df3

Output

   x3 y3
1 21 39
2 24 36
3 24 31
4 22 46
5 25 27
6 24 29
7 24 30
8 23 26
9 24 45
10 22 37
11 23 35
12 23 43
13 22 38
14 22 32
15 25 49
16 23 44
17 24 34
18 21 40
19 21 47
20 25 42

Example

 Live Demo

x4<−sample(24:25,20,replace=TRUE)
y4<−sample(41:50,20,replace=TRUE)
df4<−data.frame(x4,y4)
df4

Output

   x4 y4
1 25 50
2 24 44
3 24 45
4 25 49
5 24 42
6 25 48
7 25 43
8 25 47
9 24 50
10 25 41
11 25 47
12 25 50
13 24 46
14 25 50
15 24 42
16 24 42
17 24 50
18 25 47
19 25 42
20 25 41
which(df3$x3 %in% df4$x4)
[1] 2 3 5 6 7 9 15 17 20
which(df3$y3 %in% df4$y4)
[1] 4 9 12 15 16 19 20
raja
Published on 07-Nov-2020 11:55:24
Advertisements