jon
jon

Reputation: 11366

Subtracting small data.frame from large data.frame by grouped a variable

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

Upvotes: 2

Views: 1002

Answers (2)

MYaseen208
MYaseen208

Reputation: 23898

This should work

newdf <- merge(x=mdf, y=blockdf, by="block", suffixes = c("",".blockmean"))
newdf$newvr <- newdf$yld-newdf$yld.blockmean
print(newdf, row.names=FALSE)
  block sn var yld yld.blockmean newvr
 1  1   1   1            10    -9
 1  2   2   2            10    -8
 1  3   3   3            10    -7
 1  4   4   4            10    -6
 1  5   5   5            10    -5
 1  6   6   6            10    -4
 1  7   7   7            10    -3
 1  8   8   8            10    -2
 1  9   9   9            10    -1
 1 10  10  10            10     0
 2 11   1  11            20    -9
 2 12   2  12            20    -8
...........................

Upvotes: 4

Ramnath
Ramnath

Reputation: 55695

This should do the trick

block_match <- match(mdf$block, blockdf$block)
transform(mdf, yld = yld - blockdf[block_match, 'yld'])

Upvotes: 4

Related Questions