Skip to content
Back to formatted view

Raw Message

Message-ID: <4FB28430.5060406@ucalgary.ca>
Date: 2012-05-15T16:28:32Z
From: Peter Ehlers
Subject: max value
In-Reply-To: <CAB6-=dTf5J3a+X1b+FfKPpzbwM=BJQ-teV=FHWMQyfN4H4dpeA@mail.gmail.com>

On 2012-05-15 08:36, Melissa Rosenkranz wrote:
> Here is an R problem I am struggling with:
> My dataset is organized like this...
>
> subject       session        variable_x       variable_y
> 01                 1                    1<interger values>
> 01                 1                    2
> 01                 1                    3
> 01                 2                    1
> 01                 2                    2
> 01                 2                    3
> 02                 1                    1
> 02                 1                    2
> 02                 1                    3
> 02                 2                    1
> 02                 2                    2
> 02                 2                    3
> 03                 1                    1
> 03                 1                    2
> 03                 1                    3
> 03                 2                    1
> 03                 2                    2
> 03                 2                    3
> ...
>
> I need to find the level of variable x at which variable y has the maximum
> value for each individual for each session. Then, I need to create another
> variable, say variable "z" that labels that row in the dataset as the "max"
> for that individual at that time. I have searched the archives and the web
> for ideas, but am having trouble finding appropriate search terms for what
> I need to do. Any advice? Thank you!!
>

This is one way:

   set.seed(123)
   d <- data.frame(
        subject = gl(3,6,labels=c("01","02","03")),
        session = gl(2,3,18),
        x = gl(3,1,18),
        y = sample(11:15, 18, replace=TRUE))

   library(plyr)
   ddply(d, .(subject, session), transform,
         z = ifelse(y == max(y), 1, 0))

Peter Ehlers