Hi > > 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:
Here is another d$z<-ave(d$y, d$subject, d$session, FUN=function(x) x==max(x)) Regards Petr > > 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 > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.