Skip to content
Prev 248623 / 398502 Next

sapply puzzlement

In addition to what has already been suggested you could use ......

mapply(function(x,y) x-y, z,means)

which returns ....

             V1         V2
[1,]  0.3333333 -2.7142857
[2,]         NA  7.2857143
[3,] -0.6666667 -3.7142857
[4,] -6.6666667         NA
[5,]         NA -0.7142857
[6,]  1.3333333  1.2857143
[7,]  3.3333333 -1.7142857
[8,]  2.3333333  0.2857143

The results you see when you use the z-means approach are caused by the
vectors being different lengths. The shorter one (means) is repeated. 

Phil Spector's book describes a nice example which illustrates the behaviour
nicely.

nums = 1:10
nums +c(1,2)

HTH

Pete