In the following, data1 is a main database. data2 is a function mapping scores to levels. I want to assign levels to data1.
data1 <- data_frame(id=1:6,score=1:6-0.1)
data2 <- data_frame(score=2:4,level=c("a","b","c"))
The final output:
id score level
1 0.9 a
2 1.9 a
3 2.9 b
4 3.9 c
5 4.9 c
6 5.9 c
Essentially,
if score < data2$score[1], level = data2$score[1].
if score > data2$score[length(data2$score)], level = data2$score[length(data2$score)].
if data2$score[i] < score < data2$score[i+1], level = data2$level[i]
Is there a way to realize this with dplyr (preferably) or base R? I know data.table may have a way to do this, but I want to seek some other options as well