I am currently trying to figure out how to select all the rows of a long dataframe (long
) that present the same x1
and x2
combinations characterizing another dataframe (short
).
The simplified data are:
long <- read.table(text = "
id_type x1 x2
1 0 0
1 0 1
1 1 0
1 1 1
2 0 0
2 0 1
2 1 0
2 1 1
3 0 0
3 0 1
3 1 0
3 1 1
4 0 0
4 0 1
4 1 0
4 1 1",
header=TRUE)
and
short <- read.table(text = "
x1 x2
0 0
0 1",
header=TRUE)
The expected output would be:
id_type x1 x2
1 0 0
1 0 1
2 0 0
2 0 1
3 0 0
3 0 1
4 0 0
4 0 1
I have tried to use:
out <- long[unique(long[,c("x1", "x2")]) %in% unique(short[,c("x1", "x2")]), ]
but the %in%
adoption is used wrongly here.. thank you very much for any help!
You are requesting an inner join: