[R] new standardised variable based on group membership
John.Morrongiello at csiro.au
John.Morrongiello at csiro.au
Tue Oct 4 01:01:45 CEST 2011
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:
> 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
>
>
>> -----Oorspronkelijk bericht-----
>> Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> Namens John.Morrongiello at csiro.au
>> Verzonden: maandag 3 oktober 2011 7:34
>> Aan: r-help at r-project.org
>> Onderwerp: [R] new standardised variable based on group membership
>>
>> Hi
>> I have a data comprised of repeated measures of growth (5-15 records per
>> individual) for 580 fish (similar to Orange dataset from nlme library). I would like
>> to standardise these growth measures (yi – ŷ/sd) using mean and standard
>> deviation unique to each fish. Can someone suggest a function that would help
>> me do this? I’ve had a look at scale and sweep but can’t find a worked example
>> that does what I’m after
>>
>> Cheers
>>
>> John
>>
>>
>> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list