My question is almost identical to this one except instead of finding the closest value between a column value and a fixed number, e.g. "2", I want to find the closest value to the value in another column.. Here's an example of data:
df <- data.frame(site_no=c("01010500", "01010500", "01010500","02010500", "02010500", "02010500", "03010500", "03010500", "03010500"),
OBS=c(423.9969, 423.9969, 423.9969, 123, 123, 123, 150,150,150),
MOD=c(380,400,360,150,155,135,170,180,140),
HT=c(14,12,15,3,8,19,12,23,10))
Which looks like this:
site_no OBS MOD HT
1 01010500 423.9969 380 14
2 01010500 423.9969 400 12
3 01010500 423.9969 360 15
4 02010500 123.0000 150 3
5 02010500 123.0000 155 8
6 02010500 123.0000 135 19
7 03010500 150.0000 170 12
8 03010500 150.0000 180 23
9 03010500 150.0000 140 10
The goal is, for every "site_no", find the closest MOD value that matches the OBS value, then return the corresponding HT. For example, for site_no 01010500, 423.9969 - 400 yields the minimum difference, and thus the function would return 12. I have tried most of the solutions from the other post, but get an error due to $ with atomic vector (the df is recursive, but I think the function is not). I tried:
ddply(df, .(site_no), function(z) {
z[abs(z$OBS - z$MOD) == min(abs(z$OBS - z$MOD)), ]
})
Error in z$River_Width..m. - z$chan_width :
non-numeric argument to binary operator