Reputation: 1
I need to loop into df2 and take the first value in df2$col1 and the last value in df2$col2 and use those values to only select the corresponding rows and all in between in df1$col1
If you can help me with this I'd really appreciate it!
df1
col1
1
2
3
4
5
etc
df2
col1 col2
1200 1250
1299 1325
1350 1500
Upvotes: 0
Views: 113
Reputation: 61204
As pointed out by SimonO101, you can use "["
and %in%
.
Try this:
df1[df1$col1 %in% head(df2,1)[1,1] : tail(df2, 1)[1,2], , drop=FALSE]
As @flodel commented you can even use <=
to be more efficient
df1[df2[1,1] <= df1$col1 & df1$col1 <= df2[nrow(df2),2], , drop=FALSE]
equivalent to
df1[df1$col1 >= df2[1,1] & df1$col1 <= df2[nrow(df2),2], , drop=FALSE]
Upvotes: 1