I am trying to find the best way to get the value of column Y which falls in the range of column Z and return the corresponding row.
For instance, consider A in column X, the value in Y is 25 so it falls between 20 and 30 so the corresponding value in W is 2.
## example dataframe
df <- data.frame(X=c("A","A","A","A","B","B","B","C","C","C"),
Y=c(25,25,25,25,35,35,35,15,15,15),
Z=c(10,20,30,40,10,20,30,10,20,30),
w=c(1,2,3,4,1,2,3,1,2,3))
#desired dataframe
df2 <- data.frame(X=c("A","B","C"),
Y=c(25,35,15),
Z=c(20,30,10),
w=c(2,3,1))
The problem needs a fix. Let´s say D has a value Y (5) less than min Z (10) in this case it would get 0. How can that be accomplished ?
df <- data.frame(X=c("A","A","A","A","B","B","B","C","C","C","D","D","D"),
Y=c(25,25,25,25,35,35,35,15,15,15,5,5,5),
Z=c(10,20,30,40,10,20,30,10,20,30,10,20,30),
w=c(1,2,3,4,1,2,3,1,2,3,1,2,3))
df2 <- data.frame(X=c("A","B","C","D"),
Y=c(25,35,15,5),
Z=c(20,30,10,0),
w=c(2,3,1,0))