I have many look-up tables in my R, which have the following format:
lookup <- data.frame('value' = c(0,50),
'return' = c("0 to 50","over 50"))
value return
1 0 0 to 50
2 50 over 50
My main dataset looks like this:
main_df <- data.frame('somecolumn'=c(10,20,100,30))
somecolumn
1 10
2 20
3 100
4 30
I need to join the two datasets using a left join with the main_df on the left, and the lookup table on the right. The match should be approximate. That is, values with a value between 0 to 50 should match to the first index of the lookup table, and those above 50 should match to the 2nd index. My actual lookup tables are much larger than this.
The result should be:
somecolumn return
1 10 0 to 50
2 20 0 to 50
3 100 over 50
4 30 0 to 50
Python can do this using the pd.merge_asof (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge_asof.html). In excel, this can be done using VLOOKUP with TRUE as the final parameter.
How can I do this in a one line function in R? The reason I need to be one line is because I have many lookup tables, and this needs to be applied a lot of times.
I think this can be done using cut
in R, which will remove the need for lookup
, but I am not sure how to get it to output a character with the bins I require