Hello I am trying to find a way to find the max value, for only a subset of a dataframe, depending on how the data is grouped for example, How would I find the maxmium responce, for all the GPR119a condition below: responce,mouce,condition 0.105902,KO,con 0.232018561,KO,con 0.335008375,KO,con 0.387025433,KO,GPR119a 0.576769897,KO,GPR119a 0.645120419,KO,GPR119a 0.2538608,KO,GPR119b 0.183061952,KO,GPR119b 0.824035587,KO,GPR119b 0.399201597,KO,GPR119c 0.417006618,KO,GPR119c 0.572958834,KO,GPR119c 0.229467444,KO,GPR119d 0.294089745,KO,GPR119d 0.309964445,KO,GPR119d 0.30474325,KO,GPR119e 0.159374839,KO,GPR119e 0.467726848,KO,GPR119e 1.01841912,KO,GPR119f 0.423028621,KO,GPR119f 0.223588597,KO,GPR119f Thank
?tapply would be one way to get the max for all conditions at once. Your example is not reproducible, so I cannot give you a reproducible answer, but adapt the following: tapply(df$responce, df$condition, max) A. Ramzan wrote:> Hello > > I am trying to find a way to find the max value, for only a subset of a > dataframe, depending on how the data is grouped for example, > > How would I find the maxmium responce, for all the GPR119a condition below: > > responce,mouce,condition > 0.105902,KO,con > 0.232018561,KO,con > 0.335008375,KO,con > 0.387025433,KO,GPR119a > 0.576769897,KO,GPR119a > 0.645120419,KO,GPR119a > 0.2538608,KO,GPR119b > 0.183061952,KO,GPR119b > 0.824035587,KO,GPR119b > 0.399201597,KO,GPR119c > 0.417006618,KO,GPR119c > 0.572958834,KO,GPR119c > 0.229467444,KO,GPR119d > 0.294089745,KO,GPR119d > 0.309964445,KO,GPR119d > 0.30474325,KO,GPR119e > 0.159374839,KO,GPR119e > 0.467726848,KO,GPR119e > 1.01841912,KO,GPR119f > 0.423028621,KO,GPR119f > 0.223588597,KO,GPR119f > > Thank > > ______________________________________________ > 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.
max(subset(dataName, condition=="GPR119a")$responce) ? ???, 01/02/2011 ? 19:05 +0000, A. Ramzan ????:> Hello > > I am trying to find a way to find the max value, for only a subset of a > dataframe, depending on how the data is grouped for example, > > How would I find the maxmium responce, for all the GPR119a condition below: > > responce,mouce,condition > 0.105902,KO,con > 0.232018561,KO,con > 0.335008375,KO,con > 0.387025433,KO,GPR119a > 0.576769897,KO,GPR119a > 0.645120419,KO,GPR119a > 0.2538608,KO,GPR119b > 0.183061952,KO,GPR119b > 0.824035587,KO,GPR119b > 0.399201597,KO,GPR119c > 0.417006618,KO,GPR119c > 0.572958834,KO,GPR119c > 0.229467444,KO,GPR119d > 0.294089745,KO,GPR119d > 0.309964445,KO,GPR119d > 0.30474325,KO,GPR119e > 0.159374839,KO,GPR119e > 0.467726848,KO,GPR119e > 1.01841912,KO,GPR119f > 0.423028621,KO,GPR119f > 0.223588597,KO,GPR119f > > Thank > > ______________________________________________ > 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.
If your data frame is called myDF, max(myDF[myDF[, condition] == "GPR119a", "responce"]) ---- Original message ---->Date: 01 Feb 2011 19:05:46 +0000 >From: r-help-bounces at r-project.org (on behalf of "A. Ramzan" <ar562 at cam.ac.uk>) >Subject: [R] (no subject) >To: r-help at r-project.org > >Hello > >I am trying to find a way to find the max value, for only a subset of a >dataframe, depending on how the data is grouped for example, > >How would I find the maxmium responce, for all the GPR119a condition below: > >responce,mouce,condition >0.105902,KO,con >0.232018561,KO,con >0.335008375,KO,con >0.387025433,KO,GPR119a >0.576769897,KO,GPR119a >0.645120419,KO,GPR119a >0.2538608,KO,GPR119b >0.183061952,KO,GPR119b >0.824035587,KO,GPR119b >0.399201597,KO,GPR119c >0.417006618,KO,GPR119c >0.572958834,KO,GPR119c >0.229467444,KO,GPR119d >0.294089745,KO,GPR119d >0.309964445,KO,GPR119d >0.30474325,KO,GPR119e >0.159374839,KO,GPR119e >0.467726848,KO,GPR119e >1.01841912,KO,GPR119f >0.423028621,KO,GPR119f >0.223588597,KO,GPR119f > >Thank > >______________________________________________ >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.-------------------------------------- Dario Strbenac Research Assistant Cancer Epigenetics Garvan Institute of Medical Research Darlinghurst NSW 2010 Australia
max(dat[dat$condition=="GPR119a", "responce"]) aggregate(dat$responce, by=list(dat$condition), max) see ?"[" and ?aggregate -Ista On Tue, Feb 1, 2011 at 2:05 PM, A. Ramzan <ar562 at cam.ac.uk> wrote:> Hello > > I am trying to find a way to find the max value, for only a subset of a > dataframe, depending on how the data is grouped for example, > > How would I find the maxmium responce, for all the GPR119a condition below: > > responce,mouce,condition > 0.105902,KO,con > 0.232018561,KO,con > 0.335008375,KO,con > 0.387025433,KO,GPR119a > 0.576769897,KO,GPR119a > 0.645120419,KO,GPR119a > 0.2538608,KO,GPR119b > 0.183061952,KO,GPR119b > 0.824035587,KO,GPR119b > 0.399201597,KO,GPR119c > 0.417006618,KO,GPR119c > 0.572958834,KO,GPR119c > 0.229467444,KO,GPR119d > 0.294089745,KO,GPR119d > 0.309964445,KO,GPR119d > 0.30474325,KO,GPR119e > 0.159374839,KO,GPR119e > 0.467726848,KO,GPR119e > 1.01841912,KO,GPR119f > 0.423028621,KO,GPR119f > 0.223588597,KO,GPR119f > > Thank > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Hi: Here are three more 'solutions' using packages doBy, plyr and data.table. ### For illustration: maxima for all mouce/GPR* conditions (1) doBy: library(doBy) dfsub <- subsetBy( ~ 1, subset = condition != 'con', data = df) summaryBy(responce ~ mouce + condition, data = dfsub, FUN = max) mouce condition responce.max 1 KO GPR119a 0.6451204 2 KO GPR119b 0.8240356 3 KO GPR119c 0.5729588 4 KO GPR119d 0.3099644 5 KO GPR119e 0.4677268 6 KO GPR119f 1.0184191 # In fact, the subsetBy() call is superfluous, since we can also use summaryBy(responce ~ mouce + condition, data = subset(df, condition !'con'), FUN = max) (2) plyr, using function ddply(): library(plyr) ddply(subset(df, condition != 'con'), .(mouce, condition), summarise, maxresp = max(responce)) <output same as doBy> (3) data.table: library(data.table) dt <- data.table(df) dt[condition != 'con', list(maxresp = max(responce)), by = 'mouce, condition'] mouce condition maxresp [1,] KO GPR119a 0.6451204 [2,] KO GPR119b 0.8240356 [3,] KO GPR119c 0.5729588 [4,] KO GPR119d 0.3099644 [5,] KO GPR119e 0.4677268 [6,] KO GPR119f 1.0184191 # Plus aggregate(), in version 2.11.0 or later - it now takes a formula, so it behaves # the same as summaryBy(): aggregate(responce ~ mouce + condition, data = subset(df, condition !'con'), FUN = max) <output same as plyr and doBy> If all you want is condition GPR119a, then either of the following work: # plyr: (summarise() replaces ddply() when there is no grouping variable) summarise(subset(df, condition == 'GPR119a'), maxresp = max(responce)) maxresp 1 0.6451204 # data.table (define dt as a data table first per above) dt[condition == 'GPR119a', list(maxresp = max(responce))] maxresp [1,] 0.6451204 Some of the earlier responses show how to get your result a little more easily for this particular problem, but when your summarization needs are more complex, these packages come in very handy. HTH, Dennis On Tue, Feb 1, 2011 at 11:05 AM, A. Ramzan <ar562@cam.ac.uk> wrote:> Hello > > I am trying to find a way to find the max value, for only a subset of a > dataframe, depending on how the data is grouped for example, > > How would I find the maxmium responce, for all the GPR119a condition below: > > responce,mouce,condition > 0.105902,KO,con > 0.232018561,KO,con > 0.335008375,KO,con > 0.387025433,KO,GPR119a > 0.576769897,KO,GPR119a > 0.645120419,KO,GPR119a > 0.2538608,KO,GPR119b > 0.183061952,KO,GPR119b > 0.824035587,KO,GPR119b > 0.399201597,KO,GPR119c > 0.417006618,KO,GPR119c > 0.572958834,KO,GPR119c > 0.229467444,KO,GPR119d > 0.294089745,KO,GPR119d > 0.309964445,KO,GPR119d > 0.30474325,KO,GPR119e > 0.159374839,KO,GPR119e > 0.467726848,KO,GPR119e > 1.01841912,KO,GPR119f > 0.423028621,KO,GPR119f > 0.223588597,KO,GPR119f > > Thank > > ______________________________________________ > 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. >[[alternative HTML version deleted]]