I want to do something similar to vlookup in R, using numeric variable as a basis.
Sample lookup table:
> Value <- c(1,1.5,2,2.5,3,3.5,4,4.5,5)
> Code <- c("A","B","C","D","E","F","G","H","I")
> Lookup_Table <- data.frame(Value, Code)
> Lookup_Table
Value Code
1 1.0 A
2 1.5 B
3 2.0 C
4 2.5 D
5 3.0 E
6 3.5 F
7 4.0 G
8 4.5 H
9 5.0 I
Sample data table:
> DataSample <- c(1.2,1,2.3,2.7,3.1,3,4.6,4.5,3.8)
> DataSample <- data.frame(DataSample)
> DataSample
DataSample
1 1.2
2 1.0
3 2.3
4 2.7
5 3.1
6 3.0
7 4.6
8 4.5
9 3.8
So from this DataSample values I want to match the corresponding Code based on the lookup table values. And if my value is for example 1.2, I want to round it up to the nearest value on the lookup table which is 1.5. So I expect to get the corresponding code for 1.5.
My desired output is:
> DataSample
DataSample Code
1 1.2 B
2 1.0 A
3 2.3 D
4 2.7 E
5 3.1 F
6 3.0 E
7 4.6 I
8 4.5 H
9 3.8 G