Sorry for the semi-convoluted title. Hopefully I can demonstrate what it is I'm getting at here. I have the following data frames (simplified down from my real data):
DF1
ID Year Grade Season Score
A 2016 6 Fall 259
B 2017 7 Fall 264
C 2016 6 Fall 263
D 2018 6 Winter 269
E 2016 8 Spring 277
DF2
Grade Season Score Pctl
6 Fall 261 1
6 Fall 264 2
7 Fall 264 9
8 Spring 267 5
6 Winter 269 6.5
8 Spring 277 3
And I want to get:
DF3
ID Year Grade Season Score Pctl
A 2016 6 Fall 259 1
B 2017 7 Fall 264 9
C 2016 6 Fall 263 1
D 2018 6 Winter 269 6.5
E 2016 8 Spring 277 3
Simply put, given the Season, Grade, and Score in DF1, I want to append the corresponding Pctl from DF2. I understand that in most cases this is as simple as using merge() and providing specified "by" arguments.
However, while the Season and Grade merge seamlessly, as they have exact values to correspond to, this is not always the case for Scores.
First, take for example ID "A". Their score is 259 which falls below the Grade:6, Season:Fall, and Score:261. In this case, I want the pctl to be assigned as 1. Similarly, when the score is above the 99 pctl score (not demonstrated here), it should be assigned as 99.
A second complication is demonstrated by ID "C" where their Score:263, falls between Pctl 1 and 2. That is, it is not a 1:1 mapping. When this occurs, I simply want to round (up or down as long as it is consistent) to the nearest score and provide that Pctl. For the purpose of this demonstration I rounded down and appended Pctl 1.
From my research, is seems as though this might be able to be done through conditional join or with the GenomicRanges Package but the solutions were unclear to me.