[R] sapply puzzlement
Pete Brecknock
Peter.Brecknock at bp.com
Fri Jan 28 02:51:07 CET 2011
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
--
View this message in context: http://r.789695.n4.nabble.com/sapply-puzzlement-tp3243520p3243583.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list