Skip to content

new standardised variable based on group membership

5 messages · ONKELINX, Thierry, John.Morrongiello at csiro.au, Baptiste Auguie

#
Dear John,

You need to combine scale with a grouping function.

data(Orange)
library(plyr)
Orange <- ddply(Orange, .(Tree), function(x){
	x$ddplyAge <- scale(x$age)[, 1]
	x
})

Orange$aveAge <- ave(Orange$age, by = Orange$Tree, FUN = scale)

all.equal(Orange$ddplyAge, Orange$aveAge)

Best regards,

Thierry
#
That works a treat Thierry, thanks! I wasn't aware of the plyr package but I like what it does- I'll put it to use work in the future.

Regards

John 

-----Original Message-----
From: ONKELINX, Thierry [mailto:Thierry.ONKELINX at inbo.be] 
Sent: Monday, 3 October 2011 6:36 PM
To: Morrongiello, John (CMAR, Hobart); r-help at r-project.org
Subject: RE: [R] new standardised variable based on group membership

Dear John,

You need to combine scale with a grouping function.

data(Orange)
library(plyr)
Orange <- ddply(Orange, .(Tree), function(x){
	x$ddplyAge <- scale(x$age)[, 1]
	x
})

Orange$aveAge <- ave(Orange$age, by = Orange$Tree, FUN = scale)

all.equal(Orange$ddplyAge, Orange$aveAge)

Best regards,

Thierry
#
More concisely,

ddply(Orange, .(Tree), transform, scaled = scale(age))

HTH,

baptiste
On 4 October 2011 11:24, <John.Morrongiello at csiro.au> wrote:
#
I like that one too Baptiste, thanks

-----Original Message-----
From: baptiste auguie [mailto:baptiste.auguie at googlemail.com] 
Sent: Tuesday, 4 October 2011 9:56 AM
To: Morrongiello, John (CMAR, Hobart)
Cc: Thierry.ONKELINX at inbo.be; r-help at r-project.org
Subject: Re: [R] new standardised variable based on group membership

More concisely,

ddply(Orange, .(Tree), transform, scaled = scale(age))

HTH,

baptiste
On 4 October 2011 11:24, <John.Morrongiello at csiro.au> wrote: