1

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

Leonhardt Guass
  • 773
  • 7
  • 24

1 Answers1

2

Use cut to bin data:

data1$result = cut(data1$score, breaks = c(-Inf, data2$score[-nrow(data2)], Inf), labels = data2$level)
data1
# # A tibble: 6 x 3
#      id score result
#   <int> <dbl> <fct> 
# 1     1   0.9 a     
# 2     2   1.9 a     
# 3     3   2.9 b     
# 4     4   3.9 c     
# 5     5   4.9 c     
# 6     6   5.9 c   
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294