0

I have two datasets.

ID<-c(1,1,2,2,2)
Size<-c(15,22,100,2,19)
df1<-data.frame(ID,Size)
>df1

  ID Size
1  1   15
2  1   22
3  2  100 
4  2    2
5  2   19

ID<-c(1,1,2,2)
MinSize<-c(1,21,1,51)
MaxSize<-c(20,50,50,150)
Label<-c("Small","Medium","Small","Medium")
df2<-data.frame(ID,MinSize,MaxSize,Label)
>df2

   ID MinSize MaxSize  Label
1  1       1      20  Small
2  1      21      50 Medium
3  2       1      50  Small
4  2      51     150 Medium

I wish to use the "ID" and "Size" values in df1 to look up the appropriate "Label" value in df2.

Desired output for df1:

ID  Size Label
1   15  Small
1   22  Medium
2   100 Medium
2   2   Small
2   19  Small
Tish
  • 23
  • 3
  • You might want to look up "non-equi join," which can be done with the `data.table` or `fuzzyjoin` packages. – Jon Spring Feb 18 '21 at 01:53
  • Thanks, Jon Spring. I tried fuzzyjoin but kept getting an "All columns in a tibble must be 1d or 2d objects" error. So I went the data.table route and got it to work with the following: df1[df2,.(ID,Size=x.Size,Label),on=.(ID,>=MinSize,Size<=MaxSize),nomatch=0L] – Tish Feb 18 '21 at 14:57

0 Answers0