I have got a very large data set
mdf <- data.frame (sn = 1:40, var = rep(1:10, 4), block = rep(1:4, each = 10),
yld = c(1:40))
I have small data set
blockdf <- data.frame(block = 1:4, yld = c(10, 20, 30, 40)) # block means
All variables in both dataset except yld are factors.
I want to subtract block means (blockdf$yld) form each mdf$yld dataset, such that the block effects should correspond to block in mdf dataframe.
for example: value 10 will be substracted from all var within
first block yld in mdf
20 - second block yld in mdf
and so on
Please note that I might have sometime unbalance number of var within the reps. So I want to write it in such way that it can handle unbalance situation
This should do the trick
block_match <- match(mdf$block, blockdf$block)
transform(mdf, yld = yld - blockdf[block_match, 'yld'])