How to extract 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 can to find 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

 Live Demo

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 values in x1 of df1 that are in x2 of df2 −

df1[which(df1$x1 %in% df2$x2), "x1"]
[1] 2 1 2 2 1 1 2 1 2 1

Finding the values in y1 of df1 that are in y2 of df2 −

df1[which(df1$y1 %in% df2$y2), "y1"]
[1] 3 2 2 4 4 3 1 5 1 1 3 5 2 4 5

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
df3[which(df3$x3 %in% df4$x4), "x3"]
[1] 24 24 25 24 24 24 25 24 25
df3[which(df3$y3 %in% df4$y4), "y3"]
[1] 46 45 43 49 44 47 42
raja
Published on 06-Nov-2020 13:56:08
Advertisements