I have a daily timeseries for a hydraulic pressure head from several monitoring wells in meters above sea level in a dataframe. For example:
hydraulic.head.mw = data.frame("date" = seq(as.Date("2020-01-01"), as.Date("2020-01-31."), by = "1 day"), "B51A1367" = rnorm(31, 11, 1), "B51A1368" = rnorm(31, 12, 1), "B51A1369" = rnorm(31, 10, 1))
In another dataframe I have the ground levels of those monitoring wells above sea level. For example:
ground.levels = data.frame("B51A1369" = 12, "B51A1368" = 13, "B51A1367" = 11)
Now I would like to have the hydraulic head for the entire column B51A1367 subtracted from the ground level value stored in ground.levels so I get the hydraulic head relative to the ground level. After this column is finished R moves to the next column, looks up the matching ground level, subtracts, and goes on to the next column.
Now I want R to achieve this based on matching the column names, not numerical indexing. So R starts out with the first column name from hydraulic.head.mw, looks for that same column name in ground.levels, takes the appropriate ground level value (which is in the first cell underneath the column header), and uses that number to subtract all the values in the column in hydraulic.head.mw from. Then R moves on to the next column until all columns have passed.
I would like to know how this can be achieved. Thank you.