I would like to de-mean the 'vector' column of the following dataframe by
factor:
set.seed(5444)
vector - rnorm(1:10)
factor - rep(1:2,5)
test.df - data.frame(factor, vector)
which is:
factor vector
1 1 -0.4963935
2 2 -2.0768182
3 1 -1.584
4 2 0.8025474
5
Hello, Vassilis,
maybe
with( test.df, ave( vector, factor, FUN = function( x) x - mean( x)))
does what you want.
-- Gerrit
On Wed, 15 Dec 2010, Vassilis wrote:
I would like to de-mean the 'vector' column of the following dataframe by
factor:
set.seed(5444)
vector - rnorm(1:10)
Hi Gerrit,
This does exactly what I want, thank you very much!
Even more, I notice that ave() uses the split/unsplit functions under the
hood, which are very useful tools as they allow to apply even more
complicated functions on a factor-by-factor basis.
best,
Vassilis
--
View this
On Dec 15, 2010, at 11:19 AM, Gerrit Eichner wrote:
Hello, Vassilis,
maybe
with( test.df, ave( vector, factor, FUN = function( x) x - mean( x)))
does what you want.
THere is also the scale function which can be called with the
parameters set to center but not scale the results:
4 matches
Mail list logo