I am looking for a way to return the first column name that is greater than or equal to x but less than the next greatest value in each row of a data frame in a new column
> df <- data.frame(Loc = c("3120", "3120", "3120"), fld = c("T1", "T2", "T3"), days = c(13, 11, 18), VE = c(10,10,10), VC = c(15,15,15), V1 = c(20,20,20)
+ )
> df
Loc fld days VE VC V1
1 3120 T1 13 10 15 20
2 3120 T2 11 10 15 20
3 3120 T3 18 10 15 20
based on Loc and fld, I want to take the values of days and find closest value in VE:V1 and print the column name of that closest value in a new column, and then calculate remaining until next greatest value.
Loc fld days VE VC V1 current.growth.stage days.to.next.stage
1 3120 T1 13 10 15 20 VE 2
2 3120 T2 11 10 15 20 VE 4
3 3120 T3 18 10 15 20 VC 2
I've seen multiple threads on using min and max values but not a list of values from a column selected in a df for reference. any help would be appreciated!
thanks.
ML