0

There is dataset

data=structure(list(x1 = structure(c(1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L), .Label = c("q", 
"r", "w"), class = "factor"), x2 = structure(c(2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("e", "w"), class = "factor"), x3 = structure(c(1L, 
1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 
2L, 2L, 2L), .Label = c("e", "q", "r"), class = "factor"), var = c(34L, 
35L, 34L, 34L, 34L, 34L, 35L, 34L, 34L, 34L, 34L, 35L, 34L, 34L, 
34L, 34L, 34L, 34L, 34L, 34L), act = c(1L, 1L, 1L, 1L, 1L, 0L, 
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("x1", 
"x2", "x3", "var", "act"), class = "data.frame", row.names = c(NA, 
-20L))

by columns x1, x2, x3 we have three groups

q   w   e
w   e   r
r   e   q

There is anothere dataset. It structure

dat= structure(list(x1 = structure(1:2, .Label = c("q", "r", "w"), class = "factor"), 
     x2 = structure(c(2L, 1L), .Label = c("e", "w"), class = "factor"), 
     x3 = structure(1:2, .Label = c("e", "q", "r"), class = "factor")), 
     class = "data.frame", row.names = c(NA,-2L), .Names = c("x1", "x2", "x3"))

I.E.

  x1 x2 x3
1  q  w  e
2  r  e  q

How do I compare the datasets data and dat In the way that, in dataset data were only observation for groups indicated in dataset dat

I.E. output

x1  x2  x3  var act
q   w   e   34  1
q   w   e   35  1
q   w   e   34  1
q   w   e   34  1
q   w   e   34  1
r   e   q   34  1
r   e   q   34  1
r   e   q   34  1
r   e   q   34  1
r   e   q   34  1

NOTE: this is testdata. There are many groups.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
psysky
  • 3,037
  • 5
  • 28
  • 64
  • @phiver, i don't want use "merge", but i didn't know that dplyr has inner_join function like in sql. Now i see. – psysky Aug 05 '18 at 11:02

1 Answers1

1

Since you would like to join both data frames in the same column names, we can do

library(dplyr)
inner_join(data, dat)

Joining, by = c("x1", "x2", "x3")
   x1 x2 x3 var act
1   q  w  e  34   1
2   q  w  e  35   1
3   q  w  e  34   1
4   q  w  e  34   1
5   q  w  e  34   1
6   r  e  q  34   1
7   r  e  q  34   1
8   r  e  q  34   1
9   r  e  q  34   1
10  r  e  q  34   1
A. Suliman
  • 12,923
  • 5
  • 24
  • 37