[R] Can the by() function return a single column?

2010-12-15 Thread Vassilis
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

Re: [R] Can the by() function return a single column?

2010-12-15 Thread Gerrit Eichner
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)

Re: [R] Can the by() function return a single column?

2010-12-15 Thread Vassilis
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

Re: [R] Can the by() function return a single column?

2010-12-15 Thread David Winsemius
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: