Many thanks, truly a simple and elegant solution! Caroline
On Sun, Sep 13, 2009 at 12:08 AM, David Winsemius <dwinsem...@comcast.net> wrote: > > On Sep 12, 2009, at 11:36 AM, caroline choong wrote: > >> Dear all, >> I have a data set as follows: >> >> ID cycle.number cycle.result >> 1 2525 1 38 >> 2 2525 2 38 >> 3 2525 3 25 >> 4 2525 4 25 >> 5 2525 5 25 >> 6 2525 6 25 >> 7 2531 1 38 >> 8 2531 2 38 >> 9 2078 1 38 >> 10 2078 2 38 >> >> I want to find out the maximum cycle.number for each ID, and later find the >> corresponding cycle.result for that cycle. >> >> I have already managed to pull out the maximum cycle by using a for loop: >> >> max.cycle <- vector() >> patients <- (levels(factor(ID))) >> for (i in 1:length(patients)) { >> >> max.cycle[i] <- max(cycle.number[(ID %in% patients[i] )]) >> >> } >> >> But i would like to know if there is a better or more elegant way of pulling >> out the maximum cycle.number for each ID? > > There is: > > ?tapply > > > patients <- read.table(textConnection(" ID > > cycle.number cycle.result > + 1 2525 1 38 > + 2 2525 2 38 > + 3 2525 3 25 > + 4 2525 4 25 > + 5 2525 5 25 > + 6 2525 6 25 > + 7 2531 1 38 > + 8 2531 2 38 > + 9 2078 1 38 > + 10 2078 2 38"), header=TRUE) > > > tapply(patients$cycle.number, patients$ID, max) > 2078 2525 2531 > 2 6 2 > > >> Perhaps without the need for using >> a for loop? >> >> Many thanks, > > > David Winsemius, MD > Heritage Laboratories > West Hartford, CT > ______________________________________________ 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.