I am currently working on two different dataframes, one of which is extremely long (long). What I need to do is to select all the rows of long whose corresponding id_type appears at least once in the other (smaller) dataset.
Suppose the two dataframes 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 = "
id_type y1 y2
1 5 6
1 5 5
2 7 9",
header=TRUE)
In practice, what I am trying to obtain is:
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
I have tried to use out <- long[long[,"id_type"]==short[,"id_type"], ], but it is clearly wrong. How would you proceed? Thanks
Just use
%in%:Look at
?"%in%".